the only way to do it is to use the EXEC function.

DECLARE @tempvar varchar(200)
DECLARE @ColumnName varchar(100)
SET @ColumnName = 'MyColumn'
SET @tempvar = 'SELECT * FROM ' + @ColumnName
EXEC(@tempvar)


Just remember that with you have to make sure that you cast non-string
variable to variable.


DECLARE @tempvar varchar(200)
DECLARE @Amount int
SET @Amount = 20
SET @tempvar = 'SELECT * FROM ThisColumn WHERE Amount = ' + CAST(@Amount AS
varchar(10))

If not SQL Server thinks you're trying to add a string and a number together
and will throw an error.





Anthony Petruzzi
Webmaster
954-321-4703
http://www.sheriff.org


-----Original Message-----
From: Kola Oyedeji [mailto:[EMAIL PROTECTED]]
Sent: Friday, March 29, 2002 4:18 AM
To: CF-Talk
Subject: Dynamic queries in stored procedures


Hi

Could anyone tell me the best way to execute dynamic queries using stored
procedures?

Thanks

Kola


______________________________________________________________________
This list and all House of Fusion resources hosted by CFHosting.com. The place for 
dependable ColdFusion Hosting.
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to