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


dynamic sql example


dynamic sql example

Author
Message
craig 84462
craig 84462
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 36
Hi there,

I was wondering if someone could tell me. I am learning dynamic sql and finding the '''s hard going and have decided to proceed with splitting them up. The following program message says its fine but I get no results window. Could someone tell me why?
cheers Craig

***************code*********************

Use AdventureWorksDW2008
go

declare @Tablename varchar(400)
declare @x varchar(200)

declare curtable cursor
for
select name
from sysobjects
where type='U'
order by name



open curtable

fetch next
from curtable
into @x

while @@FETCH_STATUS=0
begin


set @x=@x+'select count(*)'+','+' '+@Tablename+' '+'from AdventureWorksDW2008.dbo.'+@Tablename

exec (@x)


fetch next
from curtable
into @Tablename

end

close curtable

deallocate curtable
Steve Cullen
Steve Cullen
Say Hey Kid
Say Hey Kid (691 reputation)Say Hey Kid (691 reputation)Say Hey Kid (691 reputation)Say Hey Kid (691 reputation)Say Hey Kid (691 reputation)Say Hey Kid (691 reputation)Say Hey Kid (691 reputation)Say Hey Kid (691 reputation)

Group: General Forum Members
Points: 691 Visits: 1226
The easiest thing to do here is to display your query (using select or print) to verify it is what you expect before executing it as a query.

Converting oxygen into carbon dioxide, since 1955.


LutzM
LutzM
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9235 Visits: 13559
You need to assign to @Tablename instead of @x in your first fetch statement.
Also, just use "set @x ='select..."
instead of "set @x =@x+'select..."



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
craig 84462
craig 84462
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 36
Thanks, but have changed it to @Tablename in first fetch statement but tried "print" and getting nothing, it seems its correct though, anything else I've missed?
craig 84462
craig 84462
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 36
cheers thanks heaps for this, used print and it does nothing. Is the set @x statement actually correct?
Bryan Oliver
Bryan Oliver
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: 51
This works should show you how to do it
Bryan

declare @Tablename varchar(400)
declare @x varchar(200)

declare curtable cursor
for
select name
from sysobjects
where type='U'
order by name



open curtable

fetch next
from curtable
into @x
set @x='select count(*) from '+@Tablename
print @x
exec (@x)
while @@FETCH_STATUS=0
begin
set @x='select count(*) from '+@Tablename
print @x
exec (@x)
fetch next
from curtable
into @Tablename

end

close curtable

deallocate curtable


Kindest Regards,

bryan.oliver@quest.com

quest.com
LutzM
LutzM
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9235 Visits: 13559
The following code worked as bad as a [c u r s o r] is supposed to but at least it gave the expected result (had to change it to AdventureWorksDW though, since I'm using SS2K5). Side note: I consider the non-set-based solution as a "valid option" for this task, but definitely not in general (I can't even type the evil word...). :-)

FETCH NEXT
FROM curtable
INTO @Tablename
WHILE @@FETCH_STATUS=0
BEGIN

SELECT @x

SET @x='select count(*)'+','+' '+@Tablename+' '+'from AdventureWorksDW2008.dbo.'+@Tablename

PRINT (@x)

FETCH NEXT
FROM curtable
INTO @Tablename

END





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
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