Recently write procedure, encounter some problems. Among them, I think the problem with "use variable name as a table name" is more valuable, written with everyone to share. If you have a good solution, you must give you a good solution.
scene:
If you want to dynamically create a Table based on the value of a specific variable when writing Procedure, the value of all or some specific variables). E.g:
Declare @tablename char (10)
// if Some Statement
Set @ TableName = 'Test'
You want to create a new table with the value of the variable @TableName as a table name, then use create table @tablename (Test Char (8)) statement, it will encounter problems,
Solve (pick up):
// If the table does not exist, create
IF not exists (Select Name from sysobjects
Where name = @ TableName and type = 'u')
Begin
Set @ cretetable = 'create table' @ Tablename '(MyName Char (2))'
Exec (@createtable)
end
Finished>