Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
shaoen01
Yak Posting Veteran
78 Posts |
Posted - 2006-01-07 : 03:37:43
|
I managed to call the deletepromotion stored procedure and wish to display the @SQL which stored the sql statement fired. This is suppose to be for debugging purposes but dont know how to do this.Another thing is after i "EXEC deletepromotion" in the query window, i received an error msg below. No doubt, it states that 1 row has been affected, when i check the table it did not delete anything. I have also included the code for my original stored procedure which did not work as well. Thanks(1 row(s) affected)Server: Msg 137, Level 15, State 2, Line 2Must declare the variable '@PROMOTIONIDS'.Stored Procedure - DebuggingCREATE PROCEDURE [DELETEPROMOTION]AsDECLARE @SQL VARCHAR(100), @PROMOTIONIDS VARCHAR (100)SET @PROMOTIONIDS ='4,5'SET @SQL="DELETE FROM MSTRPROMWHERE ',' + @PROMOTIONIDS + ',' LIKE '%,' + CONVERT(VARCHAR(20), PROMOTIONID) + ',%'"SELECT @SQLEXEC(@SQL)GO Stored Procedure - OriginalCREATE PROCEDURE [DELETEPROMOTION](@PROMOTIONIDS VARCHAR (100))AsDELETE FROM MSTRPROMWHERE ',' + @PROMOTIONIDS + ',' LIKE '%,' + CONVERT(VARCHAR(20), PROMOTIONID) + ',%'GO |
|
|
shaoen01
Yak Posting Veteran
78 Posts |
Posted - 2006-01-07 : 03:47:56
|
I did this instead and it works for deleting. But i am still unable to do anything for the debugging. ThanksStored Procedure - Modified DeleteCREATE PROCEDURE [DELETEPROMOTION](@PROMOTIONIDS VARCHAR (100))AsDELETE FROM MSTRPROMWHERE PROMOTIONID IN (''+@PROMOTIONIDS+'')GO |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-01-07 : 09:46:08
|
| It doesn't work for deleting if you have a csv list in @PROMOTIONIDSyou needwhere ',' + @PROMOTIONIDS + ',' like '%,' + convert(varchar(20),PROMOTIONIDS) + ',%' for your dynamic sql it should be select @SQL='DELETE FROM MSTRPROMWHERE PROMOTIONID in '(' + @PROMOTIONIDS + ')'exec (@sql)you could also use a function to split the csv string into a table and join to that (probably the best solutionseehttp://www.nigelrivett.net/SQLTsql/ParseCSVString.htmlIt is a single statement so debug won't help you.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-07 : 10:21:39
|
I think this should work - suggestions are in red. The SET NOCOUNT thingie will stop the "1 row(s) processed" messages, which would otherwise probably muck up your application layer!CREATE PROCEDURE dbo.[DELETEPROMOTION]AsSET NOCOUNT ONDECLARE @SQL VARCHAR(100), @PROMOTIONIDS VARCHAR (100)SET @PROMOTIONIDS ='4,5'SET @SQL="DELETE FROM MSTRPROMWHERE ',' + @PROMOTIONIDS + ',' LIKE '%,' + CONVERT(VARCHAR(20), PROMOTIONID) + ',%'"SELECT @SQLEXEC(@SQL)DELETE FROM dbo.MSTRPROMWHERE ',' + @PROMOTIONIDS + ',' LIKE '%,' + CONVERT(VARCHAR(20), PROMOTIONID) + ',%'SET NOCOUNT OFFGO Kristen |
 |
|
|
shaoen01
Yak Posting Veteran
78 Posts |
Posted - 2006-01-07 : 14:48:11
|
Thanks for the suggestion guys. Appreciate it! I think i stick to modified delete stored procedure. Anybody have any comments on the downside of it or any way i can improve it, i am at all ears. The below code works great for me btw.I have also taken into account the "SET NOCOUNT ON" and "SET NOCOUNT OFF" and placed it inside any stored procedure. Please take a look and to see if i placed it correctly.CREATE PROCEDURE [DELETEPROMOTION](@PROMOTIONIDS VARCHAR (100))AsDELETE FROM MSTRPROMWHERE PROMOTIONID IN (''+@PROMOTIONIDS+'')GOCREATE PROCEDURE [ADDPROMOTION] (@PROMOTIONNAME VARCHAR (100),@PROMOSTARTDATE DATETIME,@PROMOENDDATE DATETIME,@DISCOUNTRATE INT,@PROMODESC VARCHAR(100),@ITEMIDS VARCHAR(200))AsSET NOCOUNT ONDeclare @PID INT, @SALEITEMID VARCHAR(10)-- INSERT the new recordINSERT INTO MSTRPROM(PROMOTIONNAME, DISCOUNTRATE, PROMOSTARTDATE,PROMOENDDATE, PROMODESC)VALUES(@PROMOTIONNAME, @DISCOUNTRATE, @PROMOSTARTDATE, @PROMOENDDATE, @PROMODESC)-- Now return the InventoryID of the newly inserted recordSET @PID = SCOPE_IDENTITY()SELECT @PIDINSERT INTO ITEMPROM(PROMOTIONID, ITEMID)SELECT @PID, [Data]FROM DBO.SPLIT(@ITEMIDS, ',')SET NOCOUNT OFFGO |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-07 : 14:54:23
|
| "Please take a look and to see if i placed it correctly"Looks fine. Basically you need to turn it ON before you have performed any operation that will return a resultset, and (for "tidiness" turn it off before you exit the Sproc)Kristen |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-01-08 : 08:54:31
|
| DELETE FROM MSTRPROMWHERE PROMOTIONID IN (''+@PROMOTIONIDS+'')this needs to beexec (DELETE FROM MSTRPROMWHERE PROMOTIONID IN ('+@PROMOTIONIDS+')')See my previous post.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-09 : 01:47:06
|
quote: SET @SQL="DELETE FROM MSTRPROMWHERE ',' + @PROMOTIONIDS + ',' LIKE '%,' + CONVERT(VARCHAR(20), PROMOTIONID) + ',%'"SELECT @SQL
Refer this thread where we already told you how to use it with and without Dynamichttp://sqlteam.com/forums/topic.asp?TOPIC_ID=59760MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-09 : 03:19:30
|
| Nigel slipped an apostrophe, but you should do it WITHOUT using dynamic SQL as we've explained.exec ('DELETE FROM MSTRPROMWHERE PROMOTIONID IN ('+@PROMOTIONIDS+')')kristen |
 |
|
|
shaoen01
Yak Posting Veteran
78 Posts |
Posted - 2006-01-09 : 04:51:27
|
| okay ... thanks a lot! |
 |
|
|
|
|
|
|
|