I’m currently creating a SP that will copy information from table a into table b.
The problem that I’m facing is that the information that will be copied can be freely defined in table c.
So for Example:
Table a has the columns test nvarchar(50), test1 nvarchar(79), beta2 nvarchar(80) and id int
In the settings table it is defined that the columns test and test1 should be copied from table a to table b.
I did include a check if the column already exists. It now fails at the following lines:
DECLARE @dataType nvarchar(100)
DECLARE @statement nvarchar(250)
set @dataType =
SELECT c.*, s.name
FROM sys.columns c
INNER JOIN sys.objects o
ON c.object_id = o.object_id
INNER JOIN sys.types s
ON c.user_type_id = s.user_type_id
WHERE o.name = 'a' AND c.name = 'test'
set @Statement = 'Alter table dbo.t_hist_Devicereport add test ' + @dataType
exec (@Statement)
The Error message is that I’ve incorrect syntax near SELECT but if I run the SELECT statement by itself it works quite fine…
I’m using SQL Server 2005 in case that could be a problem
1 Answer