Dynamic table names [closed]
-
26-02-2021 - |
Pergunta
I am trying to create dynamic table names for each month.
This is how I tried it:
declare @query varchar(max)
declare @i date
declare @tableid int
declare @tblname varchar(20)
declare @fulltablename varchar(50)
set @tblname = 'tblLoan_'
set @i = '07/01/2019'
while @i <= (select max(Approval_date) from tblLoan)
begin
set @tableid = month(@i)
set @fulltablename = @tblname+ cast(@tableid as varchar)
set @query = '(select * into ['+@fulltablename+'] from tblLoan
where
Approval_date <= '+ @i + ')'
execute (@query)
set @i = dateadd(month, +1, @i)
end
I am getting this error:
Msg 402, Level 16, State 1, Line 20
The data types varchar and date are incompatible in the add operator.
After concentrating @i
variable:
declare @query varchar(max)
declare @i date
declare @tableid int
declare @tblname varchar(20)
declare @fulltablename varchar(50)
set @tblname = 'tblLoan_'
set @i = '07/01/2019'
while @i <= (select max(Approval_date) from tblLoan)
begin
set @tableid = month(@i)
set @fulltablename = @tblname+ cast(@tableid as varchar)
set @query = '(select * into ['+@fulltablename+'] from tblLoan
where
Approval_date <= '+ convert(varchar(10),@i) + ')'
execute (@query)
set @i = dateadd(month, +1, @i)
end
Now I am getting this for every loop:
Msg 206, Level 16, State 2, Line 1
Operand type clash: datetime2 is incompatible with int
I got an exam like this in my course. I am taking a course for data analytics.
Is this a bad idea to implement in a data warehouse? If yes, why?
Solução
You need to convert @i
to a character datatype before you can concatenate it into the @query
variable.
Example with a print
statement included and the exec
commented out
declare @tblLoan table (Approval_date date)
insert into @tblLoan(Approval_date) values('2019-08-01')
declare @query varchar(max)
declare @i date
declare @tableid int
declare @tblname varchar(20)
declare @fulltablename varchar(50)
set @tblname = 'tblLoan_'
set @i = '07/01/2019'
while @i <= (select max(Approval_date) from @tblLoan)
begin
set @tableid = month(@i)
set @fulltablename = @tblname+ cast(@tableid as varchar)
set @query = '(select * into ['+@fulltablename+'] from tblLoan
where
Approval_date <= '+ convert(varchar(10),@i) + ')'
print @query
--execute (@query)
set @i = dateadd(month, +1, @i)
end
(1 row affected)
(select * into [tblLoan_7] from tblLoan
where
Approval_date <= 2019-07-01)
(select * into [tblLoan_8] from tblLoan
where
Approval_date <= 2019-08-01)
To get around the operand clash error between int
and datetime2
, alter your dynamic code to ensure your @i
date is wrapped in quotes:
Approval_date <= '''+ convert(varchar(10),@i) + ''')'
Example:
create table tblLoan (Approval_date date)
insert into tblLoan(Approval_date) values('2019-08-01')
declare @query varchar(max)
declare @i date
declare @tableid int
declare @tblname varchar(20)
declare @fulltablename varchar(50)
set @tblname = 'tblLoan_'
set @i = '07/01/2019'
while @i <= (select max(Approval_date) from tblLoan)
begin
set @tableid = month(@i)
set @fulltablename = @tblname+ cast(@tableid as varchar)
set @query = '(select * into ['+@fulltablename+'] from tblLoan
where
Approval_date <= '''+ convert(varchar(10),@i) + ''')'
print @query
execute (@query)
set @i = dateadd(month, +1, @i)
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a dba.stackexchange