1

Topic: ALTER TABLE dbo. [string_var] ADD name_report nvarchar (100) to derive a table name

Hello.
Prompt, please, as it will be correct a construction in title?
Wrote the code a new column adding in tables.
DEALLOCATE some_cursor
declare some_cursor cursor
for
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
open some_cursor
declare @string_var varchar (150)
fetch next from some_cursor INTO @string_var
WHILE @@ FETCH_STATUS = 0
BEGIN
if columnproperty (object_id (@string_var), ' name_report ', ' AllowsNull ') is null
ALTER TABLE dbo. [string_var] ADD name_report nvarchar (100);
print @string_var
fetch next from some_cursor INTO @string_var
END
CLOSE some_cursor
DEALLOCATE some_cursor

2

Re: ALTER TABLE dbo. [string_var] ADD name_report nvarchar (100) to derive a table name

I receive the message:
The message 4902, level 16, state 1, line 23
It was not possible to find object "dbo.@string_var" as it does not exist, or there are resolutions.
I understand that somehow it is necessary to address to variable value, as it correctly to make to substitute in construction ALTER TABLE

3

Re: ALTER TABLE dbo. [string_var] ADD name_report nvarchar (100) to derive a table name

alexey777 wrote:

I Understand that somehow it is necessary to address to variable value, as it correctly to make to substitute in construction ALTER TABLE

It is necessary to fulfill a line using EXEC

EXEC (' ALTER TABLE dbo. [' + @string_var + '] ADD name_report nvarchar (100); ')

Only is accurater with sql-injection

4

Re: ALTER TABLE dbo. [string_var] ADD name_report nvarchar (100) to derive a table name

alexey777;

DECLARE @sql NVARCHAR (MAX);
SET @sql = N ";
SELECT @sql = @sql + N'ALTER TABLE ' + QUOTENAME ([TABLE_SCHEMA]) + N '. ' + QUOTENAME ([TABLE_NAME]) + N'ADD name_report nvarchar (100); ' + NCHAR (13) + NCHAR (10)
FROM INFORMATION_SCHEMA.TABLES;
PRINT @sql;
--EXEC (@sql);

5

Re: ALTER TABLE dbo. [string_var] ADD name_report nvarchar (100) to derive a table name

THANKS!!!