我需要帮助动态sql查询的主体。 现在,我的查询就像这样
declare @query nvarchar(MAX), @preSubQuery nvarchar(200), @subQueryTxt nvarchar(500), @postSubQuery nvarchar(100);
set @postSubQuery = N' ELSE 0 END))'
set @preSubQuery = N' ,SUM(( CASE DATEDIFF(WEEK, pay.dateCreate, back.dateCreate) WHEN ';
set @subQueryTxt = N' THEN
CASE
WHEN ...
THEN 0
WHEN ...
THEN ...
ELSE
CASE
WHEN ...
THEN ...
WHEN ...
THEN ...
ELSE 0
END
END ';
select @query = N'select DATEPART(ISO_WEEK, pay.dateCreate) backWeek ,SUM(pay_and_back.PayActualPrincipalAmt) AS TotalAmount' +
+ N' @preSubQuery' + N' 0' + ' @subQueryTxt' + ' @postSubQuery'
+ N' @preSubQuery' + N' 1' + ' @subQueryTxt' + ' @postSubQuery'
+ N' @preSubQuery' + N' 2' + ' @subQueryTxt' + ' @postSubQuery'
....
+ N' @preSubQuery' + N' 12' + ' @subQueryTxt' + ' @postSubQuery'
select @query = @query + N'
from FinDocPayToPayback pay_and_back
left join FinDocument pay on pay.id = pay_and_back.PayID
left join FinDocument back on back.id = pay_and_back.PaybackID
group by DATEPART(ISO_WEEK, pay.dateCreate) order by 1';
select @query
EXECUTE sp_executesql @query
,N'@preSubQuery nvarchar(200)', @preSubQuery
,N'@subQueryTxt nvarchar(500)', @subQueryTxt
,N'@postSubQuery nvarchar(100)', @postSubQuery;
但我收到错误Incorrect syntax
。首先,我需要有关此查询的帮助。第二,
任何人都可以提供有关如何构建复杂动态查询的更多信息吗?
答案 0 :(得分:1)
这是你的问题。介于....
之间的某处,这会导致问题。
每当您遇到问题或出错时,每次都可以使用print @query
。
declare @query nvarchar(MAX), @preSubQuery nvarchar(200), @subQueryTxt nvarchar(500), @postSubQuery nvarchar(100);
set @postSubQuery = N' ELSE 0 END))'
set @preSubQuery = N' ,SUM(( CASE DATEDIFF(WEEK, pay.dateCreate, back.dateCreate) WHEN ';
set @subQueryTxt = N' THEN
CASE
WHEN ...
THEN 0
WHEN ...
THEN ...
ELSE
CASE
WHEN ...
THEN ...
WHEN ...
THEN ...
ELSE 0
END
END ';
select @query = N'select DATEPART(ISO_WEEK, pay.dateCreate) backWeek ,SUM(pay_and_back.PayActualPrincipalAmt) AS TotalAmount' +
+ N' @preSubQuery' + N' 0' + ' @subQueryTxt' + ' @postSubQuery'
+ N' @preSubQuery' + N' 1' + ' @subQueryTxt' + ' @postSubQuery'
+ N' @preSubQuery' + N' 2' + ' @subQueryTxt' + ' @postSubQuery'
+ N' @preSubQuery' + N' 12' + ' @subQueryTxt' + ' @postSubQuery'
select @query = @query + N'
from FinDocPayToPayback pay_and_back
left join FinDocument pay on pay.id = pay_and_back.PayID
left join FinDocument back on back.id = pay_and_back.PaybackID
group by DATEPART(ISO_WEEK, pay.dateCreate) order by 1';
select @query
这是执行时上述查询的实际输出
SELECT Datepart(iso_week, pay.datecreate) backweek ,
Sum(pay_and_back.payactualprincipalamt) AS totalamount @preSubQuery 0 @subQueryTxt @postSubQuery @preSubQuery 1 @subQueryTxt @postSubQuery @preSubQuery 2 @subQueryTxt @postSubQuery @preSubQuery 12 @subQueryTxt @postSubQuery
FROM findocpaytopayback pay_and_back
LEFT JOIN findocument pay
ON pay.id = pay_and_back.payid
LEFT JOIN findocument back
ON back.id = pay_and_back.paybackid
GROUP BY datepart(iso_week, pay.datecreate)
ORDER BY 1
对于复杂的动态查询,这是要理解的示例。 的示例1 强>
--declare table1 table ( id int, value varchar(10) )
--drop table Table1
begin tran
create table table1 ( id int, value varchar(10) )
insert into table1 values( 1,'001')
insert into table1 values(2, '002')
insert into table1 values( 3,'003')
insert into table1 values( 4,'004')
create table table2 ( id int, value varchar(10) )
insert into table2 values( 1,'t2value1')
insert into table2 values(2, 't2value2')
insert into table2 values( 3,'t2value3')
insert into table2 values( 4,'t2value4')
-----1st example
declare @sql nvarchar(max) , @temp varchar(50) = '1,2,3'
select * from table1
set @sql = 'select * from table1 where id in ( ' + @temp + ')'
declare @join_table varchar(max)= 'table2', @whereCondition varchar(max) = ' where ' , @orderby varchar(max) = ' t1.id'
select @sql
exec sp_executesql @sql
-----2nd example
set @sql = 'select * from table1 t1 inner join ' + @join_table + ' t2 on t1.id = t2.id '
--if @parameter != null you can give above this condition for
set @whereCondition = @whereCondition + ' t1.id in ( ' + @temp + ')'
--if @orderby != null order by condition
set @orderby = ' order by '+ @orderby
set @sql = @sql + @whereCondition + @orderby
select @sql --you can check that what is wrong by
exec sp_executesql @sql
rollback
**示例2:将参数传递给动态查询**
--begin tran
--create table table1 ( id int, value varchar(10) )
--insert into table1 values( 1,'001')
--insert into table1 values(2, '002')
--insert into table1 values( 3,'003')
--insert into table1 values( 4,'004')
declare @sql nvarchar(max) , @temp nvarchar(50) = '1,2,3', @tempIntSingleValue nvarchar(50) = '2'
select * from table1
set @sql = 'select * from table1 where id in ( ' + @temp + ')'
print @sql
exec sp_executesql @sql
set @sql = 'select * from table1 where id in ( @tempInner)'
print @sql
exec sp_executesql @sql , N'@tempInner int', @tempInner = @tempIntSingleValue
--rollback