I have this piece of code and instead of repeating the table name in each field every time I run the query, I just want to set the table name to a variable and use that, the code I have doesn't work, can anyone point me in the right direction?
DECLARE @table_name varchar(20)
SET @table_name = 'test_table'
IF object_id(@tableName) is not null -- object_id('TABLE_NAME')
PRINT 'Table Exists'
ELSE
CREATE TABLE [@tableName]( --TABLE DOES NOT EXIST, THIS WILL EXECUTE
[ContactID_ContactID] [tinyint] NOT NULL,
[OrganisationID_OrganisationID] [int] NOT NULL,
[Note] [nchar](300) NULL
)
This just looks for if the table exists and if it doesnt, it will create the table