Wednesday, March 7, 2012

Run a query that I have stored in a table cell

This is embarasing.

I'm creating a 500 line query from XML, CSV's, etc. and I can't fingure this out.

I'm building a very complex "SELECT" query and then shoving it into the [Select] column in a SQL 2005 table. I want to call a stored procedure that just "executes" that query and returns the results...

Any help would rock.

Gah, can't believe I didn't get it.

I was trying:

EXECUTE ((SELECT [Select] FROM [Item].[Filters] WHERE FilterID = @.FilterID))

And it needed to be:

DECLARE @.Select VARCHAR(5000)
SET @.Select = (SELECT [Select] FROM [Item].[Filters] WHERE FilterID = @.FilterID)
EXECUTE (@.Select)|||

I dont know if i have interpreted your query correctly but is this what you are looking for:

CREATEPROCEDURE [dbo].[uspMyDynamicQuery]

(

@.ColumnsASVARCHAR(400)--pass the columns in like field1, field2, field3 and etc.

)

AS

SETNOCOUNTON;

DECLARE @.SQLASVARCHAR(4000)

SET @.SQL= N'SELECT '+ @.Columns+ N' FROM tbl'

EXEC(@.SQL)

|||

Thanks for the reply :)

I have it working in the 2nd post. I basically did the same thing you did, but rather than build @.SQL, I just retrieve it.

No comments:

Post a Comment