2

Given following snippet (MS SQL):

DECLARE UpdateList CURSOR FOR
SELECT MyColumn FROM MyTable
OPEN UpdateList

Nothing fancy so far. Now I would like to declare two variables where I can write the column's and table's name into. Following, of course, wouldn't work. How can I achieve this?

DECLARE @TableName nchar(20) = 'MyTable'
DECLARE @ColumnName nchar(20) = 'MyColumn'
DECLARE UpdateList CURSOR FOR
SELECT @ColumnName FROM @TableName
OPEN UpdateList

Thx for any tipps sl3dg3

1

2 Answers 2

4

You'll have to use Dynamic SQL - you can't use parameters as table or column names. So something like:

CREATE TABLE #temp (newcol nvarchar(500)) -- Use the type you're getting out of @TableName
DECLARE @TableName nchar(20) = 'MyTable'
DECLARE @ColumnName nchar(20) = 'MyColumn'

EXEC('INSERT INTO #temp SELECT [' + @ColumnName + '] FROM [' + @TableName + ']')

DECLARE UpdateList CURSOR FOR
SELECT newcol FROM #temp
OPEN UpdateList

Please keep in mind the security and performance issues associated with dynamic SQL - I don't know how you'll be populating the variables, here, and there can be some definite danger in doing this.

EDIT: Added full code.

5
  • Thx - It's only a script for my personal use, so no security risk here. Can you give me an example how to use this with the cursor?
    – sl3dg3
    Commented Apr 26, 2011 at 14:31
  • Added - had to create the temp table so that you can select against it, if you create it in the Dynamic SQL your cursor won't be able to see it (unless you use global cursors). Commented Apr 26, 2011 at 14:43
  • Looks great - only that I can't select newcol for some reason!? In the select-statement (second-last line), following error appears: Invalid column name 'newcol'.
    – sl3dg3
    Commented Apr 26, 2011 at 14:55
  • Make sure the name of the column you create in the CREATE TABLE statement matches the name of the column you're selecting from the temp table. If you need, you can check the schema of your temp table with exec tempdb..sp_columns #temp. Commented Apr 26, 2011 at 15:01
  • I needed to drop #temp, then it worked (seems that there was the wrong name contained before). - thx
    – sl3dg3
    Commented Apr 26, 2011 at 15:21
0

This code is a very good sample for dynamic column with cursor, since you can not use '+' in @statement.

ALTER PROCEDURE dbo.spTEST
AS
    SET NOCOUNT ON
    DECLARE @query NVARCHAR(4000) = N''
    DECLARE @inputList NVARCHAR(4000) = ''
    DECLARE @field sysname = 'fn'
    DECLARE @my_cur CURSOR
    EXECUTE SP_EXECUTESQL
        N'SET @my_cur = CURSOR FAST_FORWARD FOR SELECT
            CASE @field
                WHEN ''fn'' then fn
            END
            FROM dbo.vCard WHERE [fn] LIKE ''%''+@query+''%'';OPEN @my_cur;',
        N'@field sysname, @query NVARCHAR(4000), @my_cur CURSOR OUTPUT',
        @field = @field,
        @query = @query,
        @my_cur = @my_cur OUTPUT
    FETCH NEXT FROM @my_cur INTO @inputList
    WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT @inputList
        FETCH NEXT FROM @my_cur INTO @inputList
    END
    RETURN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.