Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Data not inserting Expand / Collapse
Author
Message
Posted Saturday, March 16, 2013 7:20 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 1,916, Visits: 2,337
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/
Post #1431889
Posted Saturday, March 16, 2013 7:24 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 1,916, Visits: 2,337
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/
Post #1431891
Posted Saturday, March 16, 2013 8:58 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 1,916, Visits: 2,337
I have solved it..... forget to initialize the IsBookelet variable value


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1431956
Posted Sunday, March 17, 2013 4:05 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:33 PM
Points: 35,259, Visits: 31,740
kapil_kk (3/16/2013)
I have solved it..... forget to initialize the IsBookelet variable value


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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1432038
Posted Tuesday, March 19, 2013 8:05 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:26 PM
Points: 12,990, Visits: 12,398
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)
Post #1432669
Posted Tuesday, March 19, 2013 8:25 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:14 AM
Points: 2,857, Visits: 5,128
kapil_kk (3/16/2013)
I have solved it..... forget to initialize the IsBookelet variable value


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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1432679
Posted Tuesday, March 19, 2013 9:44 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:33 PM
Points: 35,259, Visits: 31,740
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
"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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1432746
Posted Tuesday, March 19, 2013 11:46 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 1,916, Visits: 2,337
Jeff Moden (3/17/2013)
kapil_kk (3/16/2013)
I have solved it..... forget to initialize the IsBookelet variable value


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/
Post #1432820
Posted Tuesday, March 19, 2013 11:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:26 PM
Points: 12,990, Visits: 12,398
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)
Post #1432830
Posted Tuesday, March 19, 2013 6:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:33 PM
Points: 35,259, Visits: 31,740
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1432963
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse