﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / dynamic sql example / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 15:26:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: dynamic sql example</title><link>http://www.sqlservercentral.com/Forums/Topic884265-391-1.aspx</link><description>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...). :-)[code="sql"]FETCH NEXTFROM curtableINTO @TablenameWHILE @@FETCH_STATUS=0BEGINSELECT  @xSET @x='select count(*)'+','+' '+@Tablename+' '+'from AdventureWorksDW2008.dbo.'+@TablenamePRINT (@x)FETCH NEXTFROM curtableINTO @TablenameEND[/code]</description><pubDate>Tue, 16 Mar 2010 16:09:26 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: dynamic sql example</title><link>http://www.sqlservercentral.com/Forums/Topic884265-391-1.aspx</link><description>This works should show you how to do itBryandeclare @Tablename varchar(400)declare @x varchar(200)declare curtable cursorforselect namefrom sysobjects where type='U'order by nameopen curtablefetch nextfrom curtableinto @xset @x='select count(*) from '+@Tablenameprint @xexec (@x)while @@FETCH_STATUS=0begin	set @x='select count(*) from '+@Tablename	print @x	exec (@x)fetch nextfrom curtableinto @Tablenameendclose curtabledeallocate curtable</description><pubDate>Tue, 16 Mar 2010 16:07:58 GMT</pubDate><dc:creator>Bryan Oliver</dc:creator></item><item><title>RE: dynamic sql example</title><link>http://www.sqlservercentral.com/Forums/Topic884265-391-1.aspx</link><description>cheers thanks heaps for this, used print and it does nothing. Is the set @x statement actually correct?</description><pubDate>Tue, 16 Mar 2010 15:52:01 GMT</pubDate><dc:creator>craig 84462</dc:creator></item><item><title>RE: dynamic sql example</title><link>http://www.sqlservercentral.com/Forums/Topic884265-391-1.aspx</link><description>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?</description><pubDate>Tue, 16 Mar 2010 15:51:04 GMT</pubDate><dc:creator>craig 84462</dc:creator></item><item><title>RE: dynamic sql example</title><link>http://www.sqlservercentral.com/Forums/Topic884265-391-1.aspx</link><description>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..."</description><pubDate>Tue, 16 Mar 2010 15:43:18 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: dynamic sql example</title><link>http://www.sqlservercentral.com/Forums/Topic884265-391-1.aspx</link><description>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.</description><pubDate>Tue, 16 Mar 2010 15:40:57 GMT</pubDate><dc:creator>Steve Cullen</dc:creator></item><item><title>dynamic sql example</title><link>http://www.sqlservercentral.com/Forums/Topic884265-391-1.aspx</link><description>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 &amp;#119;indow. Could someone tell me why?cheers Craig***************code*********************Use AdventureWorksDW2008godeclare @Tablename varchar(400)declare @x	 varchar(200)declare curtable cursorforselect namefrom sysobjects where type='U'order by nameopen curtablefetch nextfrom curtableinto @xwhile @@FETCH_STATUS=0beginset @x=@x+'select count(*)'+','+' '+@Tablename+' '+'from AdventureWorksDW2008.dbo.'+@Tablenameexec (@x)					fetch nextfrom curtableinto @Tablenameendclose curtabledeallocate curtable</description><pubDate>Tue, 16 Mar 2010 15:32:51 GMT</pubDate><dc:creator>craig 84462</dc:creator></item></channel></rss>