I want to insert:
TEST
, table SUBJECT
, field aSubject
when @Database = 'TS'
TEST1
, table SUBJECT
, field aSubject
when @Database = 'TS1'
DEMO
, table SUBJECT
, field aSubject
I tried this:
DECLARE @Database varchar(10)
Set @Database = 'TS'
INSERT INTO
(
CASE
WHEN @Database = 'TS' THEN 'TEST.dbo.SUBJECT'
WHEN @Database = 'TS1' then 'TEST1.dbo.SUBJECT'
ELSE 'DEMO.dbo.SUBJECT' END
) (aSubject)
SELECT 'Company'
I get error:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '('.
DECLARE @Database SYSNAME;
Set @Database = 'TS'
SET @Database = CASE
WHEN @Database = 'TS' THEN 'TEST'
WHEN @Database = 'TS1' THEN 'TEST1'
ELSE 'DEMO'
END
DECLARE @sql NVARCHAR(MAX);
SET @sql= N' INSERT INTO ' + QUOTENAME(@Database) + N'.[dbo].[SUBJECT] '
+ N' SELECT ''Company'' '
EXECUTE sp_executesql @sql