SQL Server 2005で条件付き注文を含むときに破損しているSQLクエリがあります。私は注文を削除すると、クエリが機能します。条件(例:p.Descriptionによる注文)によって注文を明示的に書くと、それは機能します。で条件付き注文を含めると、エラーが表示されます。SQL Serverの条件付き注文者
'Conversion failed when converting character string to smalldatetime data type'
SQL Serverはこのエラーの原因となったコード行を表示していません。私はどのように私は条件付きの順序を使用するか、変換で失敗している列のトラブルシューティングをすることができますこれを修正することができますかと思います。
declare @SearchTerm nvarchar(255)
declare @SortBy nvarchar(255)
declare @Months int
declare @VendorID int
declare @ProductID int
set @SearchTerm = 'focus'
set @SortBy = 'product'
set @Months = 3
set @VendorID = null
set @ProductID = null
-- This makes it so the @Month will filter by n number of months ago.
declare @PreviousMonths datetime
if @Months is null
begin
set @PreviousMonths = 24
end
else
begin
set @PreviousMonths = DateAdd(month, [email protected], GetDate())
end
select
a.dsAlertID as AlertID,
a.ProductID,
v.VendorID,
p.Description as ProductName,
v.LongName as VendorName,
a.Introduction,
a.Writeup,
a.DateAdded
from
ev_ds_Alerts a
left outer join
tblProducts p on a.ProductID = p.ProductID
left outer join
tblVendors v on v.VendorID = p.VendorID
where
(@SearchTerm is null or (a.Writeup like '% ' + @SearchTerm + '%' or a.Introduction like '% ' + @SearchTerm + '%'))
and ((@Months is null) or (@Months is not null and a.DateAdded >= @PreviousMonths))
and ((@VendorID is null) or (@VendorID is not null and v.VendorID = @VendorID))
and ((@ProductID is null) or (@ProductID is not null and p.ProductID = @ProductID))
order by
case @SortBy
when 'product' then p.Description
when 'vendor' then v.LongName
else a.DateAdded
end
-- order by p.Description or v.LongName works when explicitly writing them out!
'CAST'を使用するee –
データ型キャストエラーを避けるために独立したcase文を作成する –