SQL Clone
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
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 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
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 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
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

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

That only seems to execute the last statement.

I guess i need to somehow loop the results.

Alan
arunyadav007
arunyadav007
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 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
SSChasing Mays
SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)

Group: General Forum Members
Points: 630 Visits: 3818
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
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 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
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 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
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 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
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

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

John
Alan Stanley
Alan Stanley
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 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