Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 January 11th, 2005, 03:26 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default Dynamic SQL

Is there a way, other than concatenatin a string and excuting it to build dynamic SQL? For example, could I pass in column names or table names as a parameter and execute:

declare @tablename varchar(50)
declare @colname varchar(20)

select @colname from @tablename
 
Old January 11th, 2005, 10:19 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Can you be a little more clear, I understand your example but not your question. Could you explain it a little more.

Jaime E. Maccou
 
Old January 12th, 2005, 12:11 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

i want to use variables as a column or table name instead of hardcoding. I don't want to have to build a sql string like:

'select * from '' + @tablename + ''

then doing an sp_executesql


 
Old January 12th, 2005, 12:24 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I don't think so, you don't have to use sp_executeSql though, you can just build your @sql string and then
Code:
exec(@sql)
.

--

Joe (Microsoft MVP - XML)
 
Old January 12th, 2005, 11:14 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Thanks for the input guys!

   It is not possible to do what I want, I have tested. Perhaps in the new release of SQL Server.
   The reason I am trying to avoid building a string then executing (as a statement or stored procedure), is because SQL server cannot compile and optimize the code. Trying to get the best perfomance. If I find any other solution, I will be happy to share.

Thanks again,

Jim





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help w/ dynamic SQL statement ssmith001 Perl 1 February 23rd, 2007 10:13 AM
Dynamic SQL sanjivbshinde ADO.NET 1 January 9th, 2007 11:45 AM
C# Sql dynamic update N4th C# 2005 0 December 20th, 2006 09:05 AM
dynamic-sql-cursor uskiranj Oracle 1 July 5th, 2004 05:54 AM
Dynamic SQL Tina Pro VB.NET 2002/2003 0 April 25th, 2004 07:26 PM





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