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


Execute a dynamic select statment


Execute a dynamic select statment

Author
Message
Alan Stanley
Alan Stanley
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 176
Can anybody help me actually execute the resultant DELETE statements in this query


drop table #incoming
CREATE TABLE #incoming
([id] [bigint] IDENTITY(1,1) NOT NULL,
[tablename] nvarchar(50),
[action] [int] NULL,
[rowid] [bigint])

INSERT #incoming (tablename,action,rowid) VALUES('#test',0,1111)
INSERT #incoming (tablename,action,rowid) VALUES('#test',0,1211)
INSERT #incoming (tablename,action,rowid) VALUES('#test',0,1311)
INSERT #incoming (tablename,action,rowid) VALUES('#test',1,1111)

drop table #test
create table #test
([tid] [bigint],
[data1] nvarchar(50),
[data2] nvarchar(50))
INSERT #test (tid,data1,data2) VALUES(1111,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1211,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1311,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1111,'foo','foo')

SELECT 'DELETE FROM ' + tablename + ' where tid =' + convert(char,rowid) from #incoming where action = 0

SELECT * from #test


arunyadav007
arunyadav007
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 299
Hello Alan,
Try this...

DECLARE @QRY varchar (255)

SELECT @QRY = 'DELETE FROM ' + tablename + ' where tid =' + convert(char,rowid) from #incoming where action = 0

--PRINT @QRY
EXEC (@QRY)
Alan Stanley
Alan Stanley
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 176
arunyadav007

That only seems to execute the last statement.

I guess i need to somehow loop the results.

Alan
arunyadav007
arunyadav007
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 299
Woh ! I thought you only need to execute the last delete statement.. But, I guess you need to execute a delete statement on all the tablenames from your temporary table.. Let me try...
toddasd
toddasd
SSC-Addicted
SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)

Group: General Forum Members
Points: 480 Visits: 3783
Change to


declare @q varchar(max)
SELECT @q = ISNULL(@q, '') + 'DELETE FROM ' + tablename + ' where tid =' + convert(char,rowid) + '; ' from #incoming where action = 0
exec(@q)




but beware the dangers of this type of dynamic sql.

Edit: Read about the dangers here http://www.sommarskog.se/dynamic_sql.html

______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
Alan Stanley
Alan Stanley
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 176
Yes i need to run the results of the SELECT 'DELETE..... statement

DELETE FROM #test where tid =1111
DELETE FROM #test where tid =1211
DELETE FROM #test where tid =1311

Got me stumped
Alan Stanley
Alan Stanley
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 176
toddasd

Bingo, thank you so much.

This is part of a database sync system. The commands are built inside a stored procedure they are not stored in tables, just the data. Like table name, action, change data.

Thanks again
arunyadav007
arunyadav007
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 299
toddasd (12/5/2012)
Change to


declare @q varchar(max)
SELECT @q = ISNULL(@q, '') + 'DELETE FROM ' + tablename + ' where tid =' + convert(char,rowid) + '; ' from #incoming where action = 0
exec(@q)




but beware the dangers of this type of dynamic sql.

Edit: Read about the dangers here http://www.sommarskog.se/dynamic_sql.html

This appears to be deleting everything from #incoming

Whereas it should not have deleted the below row as action != 0

4   #test   1   1111
John Mitchell-245523
John Mitchell-245523
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7367 Visits: 15092
Put a PRINT @q in your script and you'll see why that's happening.

John
Alan Stanley
Alan Stanley
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 176
Hi

Bum data

INSERT #test (tid,data1,data2) VALUES(1111,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1211,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1311,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1111,'foo','foo')

Should read

INSERT #test (tid,data1,data2) VALUES(1111,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1211,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1311,'foo','foo')
INSERT #test (tid,data1,data2) VALUES(1411,'foo','foo')
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