sql server - Use variable to create new table in ms sql -


create proc city_info @stateref nvarchar(20) begin declare @statecod nvarchar(3); declare @check int; select @statecod = statecod state_cod state_nam = @stateref create table c0ncat(@statecod' ,'city')(sno int identity(1,1)) end 

can tell how can fetch particular name column , make table using procedure in mssql?

first of looks classic example of select * sales + @yymm

this variation of previous case, there suite of tables describe same entity. tables have same columns, , name includes partitioning component, typically year , month. new tables created new year/month begins.

in case, writing 1 stored procedure per table not feasible. not least, because user may want specify date range search, 1 procedure per table still need dynamic dispatcher.


if still want go way use dynamic-sql.

create proc city_info      @stateref nvarchar(20) begin  declare @statecod nvarchar(3); declare @check int; select @statecod = statecod state_cod state_nam = @stateref;  declare @sql nvarchar(max) =  'create table '      +  quotename(c0ncat(@statecod ,'city'))      + '(sno int identity(1,1))';  exec sp_executesql @sql end 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -