You are here: Re: The select statement is in a field « MsSQL Server « IT news, forums, messages
Re: The select statement is in a field

Posted by Erland Sommarskog on 10/01/67 11:25

George o (george.nospamogrady@hp.com) writes:
> Ok, I inherited this database and there is a field that stopres a select
> statement. Is there anyway possible to execute the value of the field
> within a select statement?
>
> For example:
>
> the table:
> Name "george"
> lookupForName "Select orders from Ordertable"
>
> So maybe something like select name, execute(lookupforname) as orders

You would have to do something like:

SELECT @sql = lookupforname FROM tbl WHERE name = 'george'
EXEC(@sql)

If you really want to add another column to that result set, it may get
ugly. If the SELECT statements does not have a WHERE clause you could
add "CROSS JOIN (SELECT name = ''george'')". Else you have to splice
it in into the query.

For more information on dynamic SQL, see this article on my web site:
http://www.sommarskog.se/dynamic_sql.html.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация