Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Data not inserting


Data not inserting

Author
Message
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2425 Visits: 2763
Hi i have written a query to check the insertion but its not working neither getting any error :

Declare @valuebased varchar(13)='0000000000'
Declare @valueDiscount varchar(13)='0000000000'
Declare @ProductDiscount varchar(13)='0000000000'
Declare @IsBooklet BIT
Declare @vouchertypeid int,
@voucherno varchar(13),
@denomination int,
@createddate datetime,
@expirydate datetime,
@voucherstatus int,
@quantity int,
@isbookletid bit
If @IsBooklet = 1
BEGIN
DECLARE @count int
SET @count =1
WHILE (@count < 10)
BEGIN
SET @voucherno = 'VVB'+CONVERT(varchar(10),MAX(@valuebased)+1)
INSERT INTO GV_Voucher (VoucherTypeId,VoucherNo, Denomination, CreatedDate, ExpiryDate,
VoucherStatusId, TransactionID, Quantity, IsBooklet)
Values
(1,@voucherno,10,GETDATE(),GETDATE(),1,'a1',12,0)
SET @valuebased = @valuebased + 1
SET @count = @count +1
END
END

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2425 Visits: 2763
This is the table structure if anyone requires:

REATE TABLE [dbo].[GV_Voucher](
[VoucherId] [int] IDENTITY(1,1) NOT NULL,
[VoucherTypeId] [int] NOT NULL,
[VoucherNo] [varchar](20) NOT NULL,
[Denomination] [int] NOT NULL,
[BookletId] [int] NULL,
[ExpiryDate] [date] NULL,
[CreatedDate] [datetime] NULL,
[ModifyDate] [datetime] NULL,
[UserId] [uniqueidentifier] NULL,
[VoucherStatusId] [int] NOT NULL,
[TransactionID] [varchar](20) NOT NULL,
[Quantity] [int] NOT NULL,
[IsBooklet] [bit] NULL,
CONSTRAINT [PK_GV_Voucher] PRIMARY KEY CLUSTERED
(
[VoucherId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2425 Visits: 2763
I have solved it..... forget to initialize the IsBookelet variable value ;-)w00t

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45163 Visits: 39924
kapil_kk (3/16/2013)
I have solved it..... forget to initialize the IsBookelet variable value ;-)w00t


I tried your code and even with the correction you posted, it does nothing because many of the other variables haven't been initialized.

If you'd like, please post your final code and we'll show you how to do this without the overhead of the loop.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16624 Visits: 17024
Jeff we have seen this same loop in several threads in the last day or two. Chris posted a great solution on one of them that got rid of the nested loops. The one posted here seems to be a shortened version of the others.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
kapil_kk (3/16/2013)
I have solved it..... forget to initialize the IsBookelet variable value ;-)w00t


You might solve it, but if you did it based on your CURSOR/LOOP solution, then it's just a great example of "How NOT to code in T-SQL"

It's especially frustrating, since you've been advised on this subject during the last few days...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45163 Visits: 39924
Sean Lange (3/19/2013)
Jeff we have seen this same loop in several threads in the last day or two. Chris posted a great solution on one of them that got rid of the nested loops. The one posted here seems to be a shortened version of the others.


BWAA-HAAA!!! Thanks, Sean. Indeed, the most difficult nested loop of them all to solve is the one involving humans. :-D

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2425 Visits: 2763
Jeff Moden (3/17/2013)
kapil_kk (3/16/2013)
I have solved it..... forget to initialize the IsBookelet variable value ;-)w00t


I tried your code and even with the correction you posted, it does nothing because many of the other variables haven't been initialized.

If you'd like, please post your final code and we'll show you how to do this without the overhead of the loop.


Hi Jeff,
Here is the code that you were asking:


Declare @vouchertype varchar(20) = 'Value Discount',
@denomination int,
@userid int,
@voucherstatus int,
@transactionID varchar(20),
@quantity int = 1,
@count int =1,
@innercount int =1,
@leaf int =10,
@max int
WHILE @count <= @quantity
BEGIN
WHILE @innercount <= @leaf
BEGIN
SET @max = (select vouchervaluebased from gv_maxvouchervalue)
SET @transactionID = (select vouchertransactionID from gv_maxvouchervalue)
if @vouchertype = (select VoucherType from GV_VoucherType where VoucherType = 'Value Based')
BEGIN
INSERT INTO GV_Booklet
VALUES
(
@leaf,
500,
1,
'VB' + Replace( Str( @max+1, 7), ' ', '0') ,
@quantity,
GETDATE(),
GETDATE(),
DATEADD(MM,6,GETDATE()),
1,
1,
'TRN' + Replace( Str( @transactionID + 1, 7), ' ', '0')
)

SET @innercount = @innercount + 1

UPDATE gv_maxvouchervalue
SET vouchervaluebased = @max + 1
END
ELSE if @vouchertype = (select VoucherType from GV_VoucherType where VoucherType = 'Value Discount')
BEGIN
INSERT INTO GV_Booklet
VALUES
(
@leaf,
500,
2,
'VD' + Replace( Str( @max+1, 7), ' ', '0') ,
@quantity,
GETDATE(),
GETDATE(),
DATEADD(MM,6,GETDATE()),
1,
1,
'TRN' + Replace( Str( @transactionID + 1, 7), ' ', '0')
)

SET @innercount = @innercount + 1

UPDATE gv_maxvouchervalue
SET vouchervaluebased = @max + 1
END
ELSE if @vouchertype = (select VoucherType from GV_VoucherType where VoucherType = 'Percentage Discount')
BEGIN
INSERT INTO GV_Booklet
VALUES
(
@leaf,
500,
3,
'PD' + Replace( Str( @max+1, 7), ' ', '0') ,
@quantity,
GETDATE(),
GETDATE(),
DATEADD(MM,6,GETDATE()),
1,
1,
'TRN' + Replace( Str( @transactionID + 1, 7), ' ', '0')
)

SET @innercount = @innercount + 1

UPDATE gv_maxvouchervalue
SET vouchervaluebased = @max + 1
END
END
--SET @quantity = @quantity - 1
SET @innercount = 1
SET @count = @count + 1
UPDATE gv_maxvouchervalue
SET vouchertransactionID = @transactionID + 1
END

select * from GV_Booklet

-- truncate table GV_Booklet

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16624 Visits: 17024
Why not just look at the excellent code that Chris M posted earlier in one of the dozen or so threads on the same topic.

http://www.sqlservercentral.com/Forums/Topic1432131-391-1.aspx#bm1432134

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45163 Visits: 39924
Sean Lange (3/19/2013)
Why not just look at the excellent code that Chris M posted earlier in one of the dozen or so threads on the same topic.

http://www.sqlservercentral.com/Forums/Topic1432131-391-1.aspx#bm1432134


Why not, indeed. Thanks, Sean. You saved me a bit o' work here.

@kapil_kk,

I read some of your replies on the thread that Sean posted a link to above. It would appear that you might want to learn the setbased way for this but you just don't understand it. Let me know if you want a short lesson on this so you can do such things on your own in the future.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search