Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

T-SQL Question
Is it possible to write a stored procedure like below where you will not kno
w
the table name in advance? The fields will always be the same. I've tried
this, setting quoted indentifier to off and cannot get it to compile. Thanks
.

Set @sql = "Select " + @siteID + " = site_id From " + @table
execute (@sql)


Report this thread to moderator Post Follow-up to this message
Old Post
Mike Collins
09-29-05 06:23 PM


RE: T-SQL Question
Mike,

You can use sp_executesql to accomplish this.

Example:

use northwind
go

declare @i int
declare @ts sysname
declare @tn sysname
declare @sql nvarchar(4000)

set @ts = 'dbo'
set @tn = 'order details'
set @sql = N'select top 1 @i = orderid from ' + quotename(@ts) + '.' +
quotename(@tn)
set @sql = @sql + N' group by orderid order by count(*) desc'

exec sp_executesql @sql, N'@i int output', @i output

print @i
go

The Curse and Blessings of Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html


AMB

"Mike Collins" wrote:

> Is it possible to write a stored procedure like below where you will not k
now
> the table name in advance? The fields will always be the same. I've tried
> this, setting quoted indentifier to off and cannot get it to compile. Than
ks.
>
> Set @sql = "Select " + @siteID + " = site_id From " + @table
> execute (@sql)
>

Report this thread to moderator Post Follow-up to this message
Old Post
Alejandro Mesa
09-30-05 01:23 AM


RE: T-SQL Question
That's great. Thanks for the example...especially for the article that looks
like it will explain everything I ever wanted to know about dynamic sql.

"Alejandro Mesa" wrote:

> Mike,
>
> You can use sp_executesql to accomplish this.
>
> Example:
>
> use northwind
> go
>
> declare @i int
> declare @ts sysname
> declare @tn sysname
> declare @sql nvarchar(4000)
>
> set @ts = 'dbo'
> set @tn = 'order details'
> set @sql = N'select top 1 @i = orderid from ' + quotename(@ts) + '.' +
> quotename(@tn)
> set @sql = @sql + N' group by orderid order by count(*) desc'
>
> exec sp_executesql @sql, N'@i int output', @i output
>
> print @i
> go
>
> The Curse and Blessings of Dynamic SQL
> http://www.sommarskog.se/dynamic_sql.html
>
>
> AMB
>
> "Mike Collins" wrote:
> 

Report this thread to moderator Post Follow-up to this message
Old Post
Mike Collins
09-30-05 01:23 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 02:27 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006