Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying for 6 hours now to write an inner join for this stored proc. For some reason no matter what I do it adds everyline of 1 table for each line of the other table.

This is my working code but as you can see, it has very low performance
USE [RHINO]
GO
/****** Object:  StoredProcedure [dbo].[spSetFeatures]    Script Date: 12/17/2011 10:31:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spSetFeatures]
-- Parameters ---------------------------------
@VehicleID int,
@VINVehicleID int,
@FeatureID int,
@VehicleValue nvarchar(100)
AS
BEGIN
Insert Into VehicleFeatures(
VehicleID,
FeatureName,
FeatureGroup,
FeatureAbbr,
FeatureCat,
FeatureUnit,
FeatureValue,
VehicleValue,
FeatureID)
Values(
@VehicleID,
(Select FeatureName From VININFO_Features WHere FeatureID=@FeatureID),
(Select GroupName From VININFO_Features Where FeatureID=@FeatureID),
(Select Abbreviation From VININFO_Features WHere FeatureID=@FeatureID),
(Select FeatureCategory From VININFO_Features Where FeatureID=@FeatureID),
(Select FeatureUnit From vwVININFO_Vehicles Where  VINVehicleID=@VINVehicleID AND FeatureID=@FeatureID),
(Select FeatureValue From vwVININFO_Vehicles Where  VINVehicleID=@VINVehicleID AND FeatureID=@FeatureID),
@VehicleValue,
@FeatureID)
--Code for spSetFeatures-------
END


I could post one of the 10,000 ways I tried to write it but if someone can please give me the answer, I will see the right way to write it as have it for use in the future
Posted

The statement you posted should add only one row. If any of the selects returns multiple rows an error is generated.

If the result is that several rows are added, are you sure you're not calling this SP repeatedly?
 
Share this answer
 
Comments
Jim Fallin 17-Dec-11 13:56pm    
OMG am I an idiot today. As I am not very skilled w/ SQL so I assumed it was a sql mistake I was making. Come to find out I wrote several correct and working inner joins but was calling them 168 times creating 28,224 new rows.

I never bothered to go back and check the code calling the SP. i just assumed I was calling it once.

Thanks for your help
Wendelius 17-Dec-11 13:59pm    
You're welcome. Think about the bright side, it's been very good practice and if you've been able to do so many variations I'd said that you're more than skilled with SQL :)
Amir Mahfoozi 17-Dec-11 23:45pm    
+5
Wendelius 18-Dec-11 2:48am    
Thanks :)
You can not use select statement for individual values of insert statement.

You should first store them in a local variable and then use that variable in your statement.

So you need to have something like this :
SQL
declare @FeatureName  varchar(max)
Select @FeatureName = FeatureName From VININFO_Features WHere FeatureID=@FeatureID
.
.
.
Insert Into VehicleFeatures(
VehicleID,
FeatureName,
FeatureGroup,
FeatureAbbr,
FeatureCat,
FeatureUnit,
FeatureValue,
VehicleValue,
FeatureID)
Values(
@VehicleID,
 @FeatureName ,
.
.
.
)



Hope it helps.
 
Share this answer
 
Comments
Wendelius 17-Dec-11 13:55pm    
That's not quite true. For example try the following:

create table inserttest (
col1 int,
col2 int
);

insert into inserttest (col1,col2) values (1, (select max(id) from sys.sysobjects));

select * from inserttest;
Amir Mahfoozi 17-Dec-11 23:44pm    
Yes your answer is more precise, but mine is not wrong.
Wendelius 18-Dec-11 2:50am    
No I didn't mean that your answer is wrong, creating variables to transfer the values to the insert statement is many times a good way.

I just wanted to point out that SELECT statements actually can be used for individual values for INSERT statement.
Amir Mahfoozi 18-Dec-11 2:54am    
Yes of course when they return a single value. Thanks :) .
Wendelius 18-Dec-11 3:00am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900