SQL存储过程求解
--查询比例if OBJECT_ID('MyInsuranceDataProc','P') IS NOT NULL
Drop Procedure MyInsuranceDataProc
go
create procedure MyInsuranceDataProc
@InsuranceCompany nvarchar(200),@InsuranceDateBeg nvarchar(100),@InsuranceDateEnd nvarchar(100),@dealerships nvarchar(100)
as
declare @Total int,@dealersum int,@sql nvarchar(2000)
--查询总数
select @Total= count(*) from CarInsuranceFollow inner join dbo.CarInsuranceTL on CarInsuranceTL.CIInsuranceCredential=CarInsuranceFollow.CF_CIInsuranceCredential
where CFGoDate between '''+convert(varchar(100),@InsuranceDateBeg,23)+''' and '''+convert(varchar(100),@InsuranceDateEnd,23)+''' and CFGoConfirm=1 and CIInsuranceCompany=@InsuranceCompany
--查询店总数
select @dealersum=count(*) from CarInsuranceFollow inner join dbo.CarInsuranceTL on CarInsuranceTL.CIInsuranceCredential=CarInsuranceFollow.CF_CIInsuranceCredential
where CFGoDate between '''+convert(varchar(100),@InsuranceDateBeg,23)+''' and '''+convert(varchar(100),@InsuranceDateEnd,23)+''' and CFGoConfirm=1 and CIInsuranceCompany=@InsuranceCompany and CFGoDealerShips=@dealerships
--计算比例
print ltrim(Convert(numeric(9,2),@dealersum*100.0/@Total))+'%'
go
这是存储过程代码,创建没有问题。
在执行时报告
消息 241,级别 16,状态 1,过程 MyInsuranceDataProc,第 7 行
从字符串转换为 datetime 时发生语法错误。
exec MyInsuranceDataProc '公司1','2013-08-01','2013-08-31','公司2'
请问大侠,具体是哪里的错误呢?
没有分了,恳请各位大侠指教! 存储 sql --------------------编程问答-------------------- UP 千万别沉了 --------------------编程问答-------------------- 你这代码肯定是从某个地方改来的,没改正确。
比如“--查询总数”,后面居然执行了拼接字符串部分,直接这样写就可以了:
select @Total= count(*) from CarInsuranceFollow inner join dbo.CarInsuranceTL on CarInsuranceTL.CIInsuranceCredential=CarInsuranceFollow.CF_CIInsuranceCredential
where CFGoDate between @InsuranceDateBeg and @InsuranceDateEnd and CFGoConfirm=1 and CIInsuranceCompany=@InsuranceCompany
同理“查询店总数”也是如此修改,再看不懂就放弃吧,对一个完全不动SQL的人来说,负担太大了。 --------------------编程问答-------------------- 对存储过程用的不是很熟。谢谢了! --------------------编程问答-------------------- 1.定义@InsuranceDateBeg nvarchar(100),@InsuranceDateEnd nvarchar(100)是字符串型,所以convert(varchar(100),@InsuranceDateBeg,23) 没有必要这样写
2.也可以@InsuranceDateBeg datetime,@InsuranceDateEnd datetime
3.exec MyInsuranceDataProc '公司1','2013-08-01','2013-08-31','公司2' 你这里没有必要在convert(23)了
4.如果表中日期数据带有时分秒,有可能查询出来的数据和你实际的不符,会少数据
补充:.NET技术 , C#