Решение, которое я нашел ... Я поместил SELECT сверху (IF) с условиями, которые не верны, и создал таблицу переменных с результатом, который он хотел выйти, а затем "ELSE" исправил ситуацию. Первая часть, только если вы понимаете вывод процесса, который я хочу. Посмотрите на мой пример
ALTER PROCEDURE [dbo].[SS_getSearchedProductsDetailsNew]
(
@mk int,
@md int,
@yr int = 0,
@caroption int = 0,
@condition int = 0,
@producttype int = 0 ,
@option int = 0,
@coloroption int = 0
)
AS
declare @sql nvarchar(max)
Begin
if @mk = 0 and @md = 0 and @yr = 0
begin
Declare @TempTable2 TABLE(
ProductID numeric(10),
TypeName nvarchar(50),
MakeID numeric(10),
ModelID numeric(10),
ConditionID numeric(10),
CarOptionsID numeric(10),
OptionsID numeric(10),
ColorOptionsID numeric(10),
Make nvarchar(50),
Model nvarchar(50),
YearID numeric(5),
Color nvarchar(50),
ProductType nvarchar(50),
CarOptionName nvarchar(50),
OptionName nvarchar(50),
ColorOptionName nvarchar(50),
ConditionName nvarchar(50),
Notes nvarchar(500),
Price money,
cog money)
select * from @TempTable2
end
else
begin
select @sql = '
declare @theNotes nvarchar(500)
declare @theMake numeric(10), @theModel numeric(10), @theYear numeric(10)
declare @makeName nvarchar(50), @modelName nvarchar(50), @ID numeric(5)
declare @theProductType nvarchar(50), @theTypeName nvarchar(50)
declare @theColor nvarchar(50),@theProductID numeric(10)
declare @theCondition numeric(10),@theCarOption numeric(10) , @theOption numeric(10), @theColorOption numeric(10)
declare @theConditionName nvarchar(50),@theCarOptionName nvarchar(50), @theOptionName nvarchar(50),@theColorOptionName nvarchar(50)
declare @thePrice money, @theCog money
declare @HoldingTable table(
ID numeric identity,
ProductID numeric(10),
MakeID numeric(10),
ModelID numeric(10),
ConditionID numeric(10),
CarOptionsID numeric(10),
OptionsID numeric(10),
ColorOptionsID numeric(10),
Make nvarchar(50),
Model nvarchar(50),
YearID numeric(5),
Color nvarchar(50),
ProductType nvarchar(50),
Notes nvarchar(500),
Price money,
cog money);
INSERT INTO @HoldingTable (ProductID,MakeID, ModelID , ConditionID, CarOptionsID,OptionsID,ColorOptionsID, Make ,Model,YearID,Color, ProductType, Notes, Price, cog)
SELECT
ProductNumber as ProductID,
tblProductsForSale.MakeID as MakeID,
tblProductsForSale.ModelID as ModelID ,
ConditionID,
CarOptionsID,
OptionsID,
ColorOptionsID,
tblVehicleMake.Make as Make ,
tblVehicleModel.Model as Model,
YearID,
Color,
ProductType, Notes,
tblProductsForSale.ResalePrice as Price,
tblProductsForSale.SellPrice as cog
from tblProductsForSale, tblVehicleMake, tblVehicleModel where
tblProductsForSale.MakeID = tblVehicleMake.MakeID and
tblProductsForSale.ModelID = tblVehicleModel.ModelID
and tblProductsForSale.ProductStatus=''available'' and tblProductsForSale.Custom=0'
if(@mk > 0)
begin
select @sql = @sql + ' and tblProductsForSale.MakeID = ' + convert(varchar, @mk)
end
if @md > 0
Begin
select @sql = @sql + ' and tblProductsForSale.ModelID = ' + convert(varchar, @md)
End
if @yr > 0
begin
select @sql = @sql + ' and tblProductsForSale.YearID = ' + convert(varchar, @yr)
end
if @caroption > 0
begin
select @sql = @sql + ' and tblProductsForSale.CarOptionsID = ' + convert(varchar, @caroption)
end
if @producttype > 0
begin
select @sql = @sql + ' and tblProductsForSale.ProductType = ''' + convert(varchar,@producttype) + ''''
end
if @option > 0
begin
select @sql = @sql + ' and tblProductsForSale.OptionsID = ' + convert(varchar, @option)
end
if @coloroption > 0
begin
select @sql = @sql + ' and tblProductsForSale.ColorOptionsID = ' + convert(varchar, @coloroption)
end
--select @sqlInsert = 'INSERT INTO @HoldingTable (ProductID,MakeID, ModelID , ConditionID, CarOptionsID,OptionsID,ColorOptionsID, Make ,Model,YearID,Color, ProductType, Price, cog) '
--select @sqlExec = @sqlInsert + @sql
--select * from @HoldingTable
select @sql = @sql + 'Declare @TempTable2 TABLE(
ProductID numeric(10),
TypeName nvarchar(50),
MakeID numeric(10),
ModelID numeric(10),
ConditionID numeric(10),
CarOptionsID numeric(10),
OptionsID numeric(10),
ColorOptionsID numeric(10),
Make nvarchar(50),
Model nvarchar(50),
YearID numeric(5),
Color nvarchar(50),
ProductType nvarchar(50),
CarOptionName nvarchar(50),
OptionName nvarchar(50),
ColorOptionName nvarchar(50),
ConditionName nvarchar(50),
Notes nvarchar(500),
Price money,
cog money)
WHILE Exists(Select * from @HoldingTable )
begin
Select @ID = ID FROM @HoldingTable
Select @theProductId = ProductID from @HoldingTable
Select @theMake = MakeID from @HoldingTable
Select @theModel = ModelID from @HoldingTable
Select @theCondition = ConditionID from @HoldingTable
Select @theCarOption = CarOptionsID from @HoldingTable
Select @theOption = OptionsID from @HoldingTable
Select @theColorOption = ColorOptionsID from @HoldingTable
Select @theYear = YearID from @HoldingTable
Select @theColor = Color from @HoldingTable
Select @theProductType = ProductType from @HoldingTable
Select @theTypeName = TypeName from tblProductType WHere ProductTypeID = cast (@theProductType as numeric(10))
Select @thePrice = Price from @HoldingTable
Select @theCog = cog from @HoldingTable
Select @theConditionName = ConditionName from tblConditions Where ConditionID = @theCondition
Select @makeName = Make from tblVehicleMake Where MakeID = @theMake
Select @modelName = Model from tblVehicleModel Where ModelID = @theModel
Select @theCarOptionName = CarOptionsName from tblCarOptions Where CarOptionsID = @theCarOption
Select @theOptionName = OptionsName from tblOptions Where OptionsID = @theOption
Select @theColorOptionName = ColorOptionsName from tblColorOptions Where ColorOptionsID = @theColorOption
Select @theNotes = Notes from @HoldingTable
Select @theProductType = ProductType from @HoldingTable
INSERT INTO @TempTable2 (ProductID,TypeName,MakeID,ModelID,ConditionID ,CarOptionsID,OptionsID ,ColorOptionsID ,Make , Model , YearID ,Color, ProductType, CarOptionName ,OptionName,ColorOptionName ,ConditionName, Notes, Price, cog)
VALUES (@theProductId,@theTypeName, @theMake, @theModel, @theCondition, @theCarOption,@theOption,@theColorOption, @makeName,@modelName, @theYear, @theColor,@theProductType, @theCarOptionName, @theOptionName, @theColorOptionName, @theConditionName, @theNotes, @thePrice , @theCog )
DELETE FROM @HoldingTable Where ID = @ID
end
Select * from @TempTable2 order by ProductID '
end
exec ( @sql )
End