Greetings,
I have a stored procedure that accepts a dynamic sql statement, and I need a way to determine the column names of the recordset generated by this sql statement. (Easy enough to do with ADO using the myRS.Fields collection, but I would like to do this entirely in my stored procedure.)
My ideas are to open a cursor using the dynamic sql statement supplied, and then examine the cursor to determine these column names. I unfortunately do not have the required permission to select from the master database table 'syscursorcolumns' which seems like the easiest way. My last resort is to try and clobber my way through the result cursor of sp_describe_cursor_columns, but this is turning out to be really ugly.
My stored proc is pasted below. A sample "@mySql" var might look something like this:
SELECT a as 'WidgetID'
, b as 'WidgetCount'
, c as 'WidgetPrice'
FROM myTable
...and what i want is to insert into another table the values "WidgetID", "WidgetCount", and "WidgetPrice", NOT the actual values represented by a, b, and c. So if someone passes another sql statment using entirely different column names, then i can capture those as well, etc...
(we can safely assume there will never be more than 9 column names in any sql statement, and i want these 9 columns to go into a single row of another table.)
The stored proc needed can be really simple:
CREATE PROCEDURE myProc
@mySql varchar(2000)
AS
BEGIN
DECLARE @ColumnName1 varchar(50)
DECLARE @ColumnName2 varchar(50)
DECLARE @ColumnName3 varchar(50)
DECLARE @ColumnName4 varchar(50)
DECLARE @ColumnName5 varchar(50)
DECLARE @ColumnName6 varchar(50)
DECLARE @ColumnName7 varchar(50)
DECLARE @ColumnName8 varchar(50)
DECLARE @ColumnName9 varchar(50)
-- some magic stuff here to retrieve column names into my declared vars
-- insert column names from the sql statement into a table
INSERT INTO myColumnsTable (Col1, Col2, ...)
VALUES (@ColumnName1, @ColumnName2, ...)
-- insert values from sql statement into another table.
-- (done easily enough with standard cursor)
END
I have a stored procedure that accepts a dynamic sql statement, and I need a way to determine the column names of the recordset generated by this sql statement. (Easy enough to do with ADO using the myRS.Fields collection, but I would like to do this entirely in my stored procedure.)
My ideas are to open a cursor using the dynamic sql statement supplied, and then examine the cursor to determine these column names. I unfortunately do not have the required permission to select from the master database table 'syscursorcolumns' which seems like the easiest way. My last resort is to try and clobber my way through the result cursor of sp_describe_cursor_columns, but this is turning out to be really ugly.
My stored proc is pasted below. A sample "@mySql" var might look something like this:
SELECT a as 'WidgetID'
, b as 'WidgetCount'
, c as 'WidgetPrice'
FROM myTable
...and what i want is to insert into another table the values "WidgetID", "WidgetCount", and "WidgetPrice", NOT the actual values represented by a, b, and c. So if someone passes another sql statment using entirely different column names, then i can capture those as well, etc...
(we can safely assume there will never be more than 9 column names in any sql statement, and i want these 9 columns to go into a single row of another table.)
The stored proc needed can be really simple:
CREATE PROCEDURE myProc
@mySql varchar(2000)
AS
BEGIN
DECLARE @ColumnName1 varchar(50)
DECLARE @ColumnName2 varchar(50)
DECLARE @ColumnName3 varchar(50)
DECLARE @ColumnName4 varchar(50)
DECLARE @ColumnName5 varchar(50)
DECLARE @ColumnName6 varchar(50)
DECLARE @ColumnName7 varchar(50)
DECLARE @ColumnName8 varchar(50)
DECLARE @ColumnName9 varchar(50)
-- some magic stuff here to retrieve column names into my declared vars
-- insert column names from the sql statement into a table
INSERT INTO myColumnsTable (Col1, Col2, ...)
VALUES (@ColumnName1, @ColumnName2, ...)
-- insert values from sql statement into another table.
-- (done easily enough with standard cursor)
END