I need to create a table where the name will be the value of 4 variables and some string.
I've found various questions/answers, but nothing that really matches what I'm after. Ultimately this will be used as embedded SQL on an eForm, but for the time being I'm trying to achieve this directly in MS SQL Server Management Studio.
This is what I have:
DECLARE @INSFIRSTNAME VARCHAR (100) = 'Ted'
DECLARE @INSSURNAME VARCHAR (100) = 'Smith'
DECLARE @INSSTAFFNO VARCHAR (10) = 'AB123'
DECLARE @INSYEAR VARCHAR (10) = '2018'
DECLARE @TABLENAME VARCHAR (400) = 'Timesheet_' + @INSFIRSTNAME + @INSSURNAME + @INSSTAFFNO + '_' + @INSYEAR
CREATE TABLE @TABLENAME
(INSFIRSTNAME VARCHAR (100),
INSSURNAME VARCHAR (100),
INSSTAFFNO VARCHAR (10),
INSYEAR VARCHAR (10));
INSERT INTO @TABLENAME
(INSFIRSTNAME, INSSURNAME, INSSTAFFNO, INSYEAR)
VALUES (@INSFIRSTNAME, @INSSURNAME, @INSSTAFFNO, @INSYEAR)
This throws up the message:
"Incorrect syntax near '@TABLENAME'.
I've tried various ways of populating the TABLENAME variable, but nothing has worked. From what I've read dynamic SQL appears to be the answer, but I haven't found a way to achieve this. Any help will be very much appreciated.
