June 8, 2006 at 6:12 pm
Hi guys,
I have multiple insert statements below
what i want is if "ProductName1" or "ProductName2" etc .. field is null then i don't want the
insert statement to execute
@ProductName nvarchar(40)
INSERT INTO Products (ProductName1 UnitPrice1
VALUES (@ProductName1 @UnitPrice1
INSERT INTO Products2 (ProductName2, UnitPrice2)
VALUES (@ProductName2, @UnitPrice2)
INSERT INTO Products3 (ProductName3, UnitPrice3)
VALUES (@ProductName3,@UnitPrice3)
INSERT INTO Products4 (ProductName4, UnitPrice4)
VALUES (@ProductName4,@UnitPrice4)
INSERT INTO Products5 (ProductName5, UnitPrice5)
VALUES (@ProductName5,@UnitPrice5)
Can i do :-
IF @ProductName = null
BEGIN
END
ELSE
BEGIN
INSERT INTO Products (ProductName1 UnitPrice1
VALUES (@ProductName1 @UnitPrice1
END
Any ideas whats the best way to tackle this?
June 8, 2006 at 6:29 pm
This:
IF @ProductName = null
Should read:
IF @ProductName is NULL
June 9, 2006 at 2:03 am
if @ProductName is not null insert Products(ProductName,UnitPrice) VALUES (@ProductName, @UnitPrice)
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
June 9, 2006 at 6:40 am
Thanks.
But the thing is i have 6 insert statements FOR example below
So can i do :-
if @ProductName,@ProductName1 is not null
insert Products(ProductName,UnitPrice)
VALUES (@ProductName, @UnitPrice)
insert Products(ProductName,UnitPrice)
VALUES (@ProductName1, @UnitPrice)
insert Products(ProductName,UnitPrice)
VALUES (@ProductName2, @UnitPrice)
insert Products(ProductName3,UnitPrice)
VALUES (@ProductName4, @UnitPrice)
insert Products(ProductName,UnitPrice)
VALUES (@ProductName5, @UnitPrice)
insert Products(ProductName,UnitPrice)
VALUES (@ProductName6, @UnitPrice)
How can i check for the 6 parameters i'm interested in?
June 9, 2006 at 6:45 am
I think you're going to have to do the check for the value prior to each individual insert.
Mark
June 9, 2006 at 1:03 pm
select @ProductName pn, @UnitPrice up union all
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
June 9, 2006 at 1:22 pm
June 9, 2006 at 1:27 pm
I may have read your request incorrectly, but it seemed as if you were asking "If any of the six product names are null, don't insert any of them." If this is the case, then:
-- If any parameter is null, this will return NULL by default
-- NOTE: Settings on the server can affect this - TEST FIRST
If (@ProductName1 + @ProductName2 + @ProductName3 + @ProductName4 + @ProductName5 + @ProductName6) Is Not Null
BEGIN
... inserts here
END
If not, then your structure is more like:
IF @ProductName1 Is Not Null
INSERT INTO Products (ProductName UnitPrice
VALUES (@ProductName1 @UnitPrice1
IF @ProductName1 Is Not Null
INSERT INTO Products (ProductName UnitPrice
VALUES (@ProductName2 @UnitPrice2
IF @ProductName1 Is Not Null
INSERT INTO Products (ProductName UnitPrice
VALUES (@ProductName3 @UnitPrice3
IF @ProductName1 Is Not Null
INSERT INTO Products (ProductName UnitPrice
VALUES (@ProductName4 @UnitPrice4
IF @ProductName1 Is Not Null
INSERT INTO Products (ProductName UnitPrice
VALUES (@ProductName5 @UnitPrice5
IF @ProductName6 Is Not Null
INSERT INTO Products (ProductName UnitPrice
VALUES (@ProductName6 @UnitPrice6
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply