1

I have the following 2 queries that should return the same information.

One normal query and the dynamic query which does not retrieve any result because of the @TABLENAME variable naming I guess.

DECLARE @TABLENAME as NVARCHAR (MAX)
SET @TABLENAME = 'MyTable'

Query Returning Results

select *  from jfa.[dbo].[MyTable]  
    INNER JOIN INFORMATION_SCHEMA.COLUMNS   on INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = @TableName and INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION = 2

Dynamic Query that does not return any result

DECLARE @sql as nvarchar(max)
set @sql = 'select *  from jfa.[dbo].[MyTable]  
INNER JOIN INFORMATION_SCHEMA.COLUMNS   on INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = ''@TableName'' and INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION = 2'

    exec sp_executeSQL @sql,
    N'@TABLENAME nvarchar',
    @TABLENAME

I guess the problem is in the following line:

 on INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = ''@TableName''

But I have tried several things an no success.

Many thanks in advance,

Kat

1

2 Answers 2

1

Problem is here:

N'@TABLENAME nvarchar',

This code means that variable @TABLENAME is nvarchar(1). Change it to e.g.

N'@TABLENAME nvarchar(45)',

Also you have to delete double ' from INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = ''@TableName''. So your code will look like:

DECLARE @sql as nvarchar(max)
set @sql = 'select *  from jfa.[dbo].[MyTable]  
INNER JOIN INFORMATION_SCHEMA.COLUMNS   on INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = @TableName --change here
and INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION = 2'

    exec sp_executeSQL @sql,
    N'@TABLENAME nvarchar(45)', --change here
    @TABLENAME

However, if you want to dynamically change table from which you are selecting data, then you have to change static jfa.[dbo].[MyTable] to jfa.[dbo].'+@TableName+'. Finally, your query will look like:

DECLARE @sql as nvarchar(max)
set @sql = 'select *  from jfa.[dbo].'+@TableName+'  --change here 
INNER JOIN INFORMATION_SCHEMA.COLUMNS   on INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = @TableName --change here
and INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION = 2'

    exec sp_executeSQL @sql,
    N'@TABLENAME nvarchar(45)', --change here
    @TABLENAME
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rokuto, I adapted it, I also defined for other Varchar (45). When inserting into the table it was just adding the first char.
1

Try this instead:

...
set @sql = 'select *  from jfa.[dbo].[MyTable]  
INNER JOIN INFORMATION_SCHEMA.COLUMNS   on INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = ' + @TableName + ' and INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION = 2'
...

You need to concatenate the parameter value directly to the dynamic sql query, so use ...' + @TableName + ' ...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.