p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > SQL Server > SQL Server 2005 > SQL Server 2005
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2005 General discussion of SQL Server *2005* version only.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 24th, 2008, 04:47 PM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get value with EXEC

Hi

I have this code:

set @sql = 'select MAX(q.eff_dt) as dw_end_dt from ' + ' (select top ' + @count_rows + '* from dbo.table where code = 99 order by eff_dt) AS q'
execute (@sql)

I need to take the value @sql and place as parameters for my other staments
set @message = ' Update dbo.table2
             Set end_dt = ''' + @sql + '''
             where eff_dt < ''' + @sql + '''
             and code = ''' + cast(@code as varchar) + ''''

Thanks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old October 24th, 2008, 05:44 PM
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 38%
Activity: 38% Activity: 38% Activity: 38%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
Default

Try something like:

DECLARE @maxdt DATETIME

set @sql = 'select @maxdt = MAX(q.eff_dt) as dw_end_dt from ' + ' (select top ' + @count_rows + '* from dbo.table where code = 99 order by eff_dt) AS q'
execute (@sql)

Update dbo.table2
Set end_dt = @maxdt
where eff_dt < @maxdt
and code = cast(@code as varchar)

************

You need to understand that @sql is that *STRING* that is the command to be executed.

You certainly wouldn't expect to use that *STRING* as the value of a date!

So you need to execute the string to produce the single result you need.

And then, since that single result is now a value of the correct datatype, you can just use it in the subsequent update.

*** UNTESTED!!! ***



No reason to use EXEC to do the update.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old October 24th, 2008, 06:41 PM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One question I need this field with this format '+ @maxdt + ' ?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old October 24th, 2008, 06:53 PM
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 38%
Activity: 38% Activity: 38% Activity: 38%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
Default

No. Definitely not.

Look, you only use the append-to-string style when you NEED to then EXEC that string.

For the UPDATE, there's no need to EXEC, so you should *NOT* be doing append-to-string.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old October 27th, 2008, 04:01 PM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This sentece with maxdt is not working, I'm receiving a syntax error when SQL try to convert the string to a date

DECLARE @maxdt DATETIME

set @sql = 'select @maxdt = MAX(q.eff_dt) as dw_end_dt from ' + ' (select top ' + @count_rows + '* from dbo.table where code = 99 order by eff_dt) AS q'
execute (@sql)

Update dbo.table2
Set end_dt = @maxdt
where eff_dt < @maxdt
and code = cast(@code as varchar)

If I use
set @sql = 'select MAX(q.eff_dt) as dw_end_dt from ' + ' (select top ' + @count_rows + '* from dbo.table where code = 99 order by eff_dt) AS q'
execute (@sql)

How can I access the result of the execute. Is there a system variable that contains the result or can I use another sentence ?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old October 27th, 2008, 06:02 PM
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 38%
Activity: 38% Activity: 38% Activity: 38%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
Default

If nobody answers you today, I'll try to look at it at home tonight. I don't have SQL Server here, right now.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old November 3rd, 2008, 04:28 PM
Friend of Wrox
Points: 751, Level: 10
Points: 751, Level: 10 Points: 751, Level: 10 Points: 751, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't understand the obsession for dynamic sql.

UPDATE t2
SET t2.end_DT = g.eff_dt
FROM Table2 AS t2
CROSS APPLY (
            SELECT eff_dt,
                ROW_NUMBER() OVER (ORDER BY eff_dt) AS recID
            FROM Table1
            WHERE code = 99
        ) AS g
WHERE g.recID = @count_rows
        AND t2.eff_dt < g.eff_dt
        AND t2.code = @code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exec plugin not found mkeehan BOOK: Beginning Spring Framework 2 ISBN: 978-0-470-10161-2 5 March 3rd, 2008 10:33 AM
create Exec File angelboy Java Basics 4 January 23rd, 2008 03:10 AM
EXEC function ajutla Beginning PHP 0 August 19th, 2004 10:18 AM
EXEC problem. pankaj_daga SQL Server 2000 8 October 4th, 2003 06:39 PM
#exec server directive vitanza BOOK: Beginning ASP 3.0 1 July 11th, 2003 10:33 AM



All times are GMT -4. The time now is 06:21 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc