March 16, 2013 at 7:20 am
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/
March 16, 2013 at 7:24 am
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/
March 16, 2013 at 8:58 pm
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/
March 17, 2013 at 4:05 pm
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
Change is inevitable... Change for the better is not.
March 19, 2013 at 8:05 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 19, 2013 at 8:25 am
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...
March 19, 2013 at 9:44 am
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.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 19, 2013 at 11:46 am
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-2 int
WHILE @count <= @quantity
BEGIN
WHILE @innercount <= @leaf
BEGIN
SET @max-2 = (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-2+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-2 + 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-2+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-2 + 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-2+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-2 + 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/
March 19, 2013 at 11:53 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 19, 2013 at 6:44 pm
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.
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
Change is inevitable... Change for the better is not.
March 19, 2013 at 11:40 pm
Jeff Moden (3/19/2013)
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.
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.
Yes sure.... will be very thankful to you for this :-):-)
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy