iccsi ( ) writes:
> I use above SQL code to generate dynamic SQL and would like to
> generate
>
> Select MyID,
> case Weekending when '2011-1-1' then MyResult,
> case Weekending when '2011-1-5' then MyResult,
> case Weekending when '2011-2-8' then MyResult
> from #w
>
> All the date is from
> "Select distinct weekending from #w"
You set @SQLString mulitple times in the loop.
You need to initiate it before the loop with "SELECT MyID". You should
add the FROM part after the loop. You also need to handle the comma, so
you don't get one too many.
--
Erland Sommarskog, SQL Server MVP, esquel.DeleteThis@sommarskog.se
Links for SQL Server Books Online:
SQL 2008:
http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:
http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx