Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Dynamic SQL

 
   Database Help (Home) -> Programming RSS
Next:  strange behavior with SP  
Author Message
iccsi

External


Since: May 05, 2011
Posts: 7



(Msg. 1) Posted: Thu May 05, 2011 8:50 am
Post subject: Dynamic SQL
Archived from groups: microsoft>public>sqlserver>programming (more info?)

DECLARE @SQLString NVARCHAR(4000)
DECLARE @MyDate NVARCHAR(50)

DECLARE cwday CURSOR for
Select distinct weekending from #w

OPEN cwday

FETCH NEXT FROM cwday INTO @MyDate

WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM cwday INTO @MyDate
SET @SQLString = N'SELECT MyID, ' + N'Case Weekending when ' +
@MyDate +
CHAR(13) + N' Then MyResult From #w '
END

print @SQLString
EXECUTE sp_executesql @SQLString

CLOSE cwday
DEALLOCATE cwday

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"

I can not get the result from above dynamic SQL.
Can you please help if I am in the wrong direction?

Your help is great appreciated,

 >> Stay informed about: Dynamic SQL 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 2) Posted: Thu May 05, 2011 6:25 pm
Post subject: Re: Dynamic SQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

 >> Stay informed about: Dynamic SQL 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Eliminating Dynamic SQL - I am refactoring stored procedures that use dynamic sql. The reason the store procedures use dynamic sql is because the data that is need comes from another MS SQL database that resides on the same server instance. The following is a code example from...

Dynamic sql and variables - Hi This may be a simple one. But anyway.... I would like to catch the result of a select statement executet as synamic sql in a variable. A simple example of what I would like to do would look like this: declare @result int, @sql_string..

Eliminating Dynamic SQL - I am refactoring stored procedures that use dynamic sql. The reason the store procedures use dynamic sql is because the data that is need comes from another MS SQL database that resides on the same server instance. The following is a code example from...

Dynamic Column Names? - Using SQL 2000 I am passing in the name of the column I want to update, @Column. When I SELECT @Column it returns the value of @Column instead of the value of the actule table column. I have tried to use ..

dynamic field name - Hi, is there any way to use dynamic field name like that ? SELECT 25 AS AA, 5 AS BB, AA*BB AS CC Thanks...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]