Wrox Programmer Forums
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2008, 03:47 PM
Registered User
 
Join Date: Oct 2008
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

 
Old October 24th, 2008, 04:44 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 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.
 
Old October 24th, 2008, 05:41 PM
Registered User
 
Join Date: Oct 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old October 24th, 2008, 05:53 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 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.
 
Old October 27th, 2008, 03:01 PM
Registered User
 
Join Date: Oct 2008
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 ?


 
Old October 27th, 2008, 05:02 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 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.
 
Old November 3rd, 2008, 04:28 PM
Friend of Wrox
 
Join Date: May 2006
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





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 09:18 AM
EXEC problem. pankaj_daga SQL Server 2000 8 October 4th, 2003 05:39 PM
#exec server directive vitanza BOOK: Beginning ASP 3.0 1 July 11th, 2003 09:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.