Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I was doing this in my stored Procedure:
SQL
        @ProductName nvarchar(50),
	@GenricName int,
	@PackingName int,
	@ShelfName int,
	@ManufactureName int,
	@TherapeuticName int
AS
	Declare @GenricKey int
	Declare @PackingKey int
	Declare @ShelfKey int
	Declare @ManufactureKey int
	Declare @TherapeuticKey int

	
Select @GenricKey = GenricKey from GenricMaster where GenricName = '@GenricName'
Select @PackingKey = PackingKey from PackingMaster where PackingName = '@PackingName'
Select   @ManufactureKey = ManufactureKey from ManufactureMaster where  ManufactureName = '@ManufactureName'
Select   @ShelfKey = ShelfKey from ShelfMaster where ShelfName = '@ShelfName'
Select   @TherapeuticKey = TherapeuticKey  from TherapeuticCategoryMaster where TherapeuticName = '@TherapeuticName'

Insert into dbo.ProductMaster
(ProductName,PackingKey,ShelfKey,ManufactureKey,GenricKey,TherapeuticKey)
values
(@ProductName,@PackingKey,@ShelfKey,@ManufactureKey,@GenricKey,@TherapeuticKey )


When i run this stored procedure it gets executed but the values in the fields i.e. in the insert query they go as null.

Please Help me out
Regards,
Mohi
Posted
Updated 14-May-12 22:14pm
v2

You don't use '' on variables. Maybe that's why your Select statements are not returning a value. Remove the '' on the variables, like this

SQL
Select @GenricKey = GenricKey from GenricMaster where GenricName = @GenricName
Select @PackingKey = PackingKey from PackingMaster where PackingName = @PackingName
Select   @ManufactureKey = ManufactureKey from ManufactureMaster where  ManufactureName = @ManufactureName
Select   @ShelfKey = ShelfKey from ShelfMaster where ShelfName = @ShelfName
Select   @TherapeuticKey = TherapeuticKey  from TherapeuticCategoryMaster where TherapeuticName = @TherapeuticName
 
Share this answer
 
Comments
Manfred Rudolf Bihy 15-May-12 4:26am    
Correct! 5+
walterhevedeich 15-May-12 4:31am    
Thanks Manfred.
Maciej Los 15-May-12 4:53am    
Good answer, my 5!
walterhevedeich 15-May-12 6:04am    
Thanks losmac.
SQL
Declare @GenricKey int
Declare @PackingKey int
Declare @ShelfKey int
Declare @ManufactureKey int
Declare @TherapeuticKey int
 
	
Select @GenricKey = GenricKey from GenricMaster where GenricName = @GenricName
Select @PackingKey = PackingKey from PackingMaster where PackingName = @PackingName
Select   @ManufactureKey = ManufactureKey from ManufactureMaster where  ManufactureName = @ManufactureName
Select   @ShelfKey = ShelfKey from ShelfMaster where ShelfName = @ShelfName
Select   @TherapeuticKey = TherapeuticKey  from TherapeuticCategoryMaster where TherapeuticName = @TherapeuticName
 
Insert into dbo.ProductMaster
(ProductName,PackingKey,ShelfKey,ManufactureKey,GenricKey,TherapeuticKey)
values
(@ProductName,@PackingKey,@ShelfKey,@ManufactureKey,@GenricKey,@TherapeuticKey )


You are searching again a string '@Whatever' instead of parameter.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 15-May-12 4:27am    
Spot on! 5+
:)
Manas Bhardwaj 15-May-12 5:16am    
Thanks!
Maciej Los 15-May-12 4:53am    
Good answer, my 5!
Manas Bhardwaj 15-May-12 5:16am    
Thanks!
You just have to remove the quotes you placed around the variables in the where clauses.
That should do the trick for you. :)
SQL
@ProductName nvarchar(50),
	@GenricName int,
	@PackingName int,
	@ShelfName int,
	@ManufactureName int,
	@TherapeuticName int
AS
	Declare @GenricKey int
	Declare @PackingKey int
	Declare @ShelfKey int
	Declare @ManufactureKey int
	Declare @TherapeuticKey int
 
	
Select @GenricKey = GenricKey from GenricMaster where GenricName = @GenricName
Select @PackingKey = PackingKey from PackingMaster where PackingName = @PackingName
Select   @ManufactureKey = ManufactureKey from ManufactureMaster where  ManufactureName = @ManufactureName
Select   @ShelfKey = ShelfKey from ShelfMaster where ShelfName = @ShelfName
Select   @TherapeuticKey = TherapeuticKey  from TherapeuticCategoryMaster where TherapeuticName = @TherapeuticName
 
Insert into dbo.ProductMaster
(ProductName,PackingKey,ShelfKey,ManufactureKey,GenricKey,TherapeuticKey)
values
(@ProductName,@PackingKey,@ShelfKey,@ManufactureKey,@GenricKey,@TherapeuticKey )


Hope that solves your issue! Besides that I would formulate the assignment part like this for clarity:

SQL
SET @GenricKey      = (SELECT GenricKey      FROM GenricMaster              WHERE GenricName = @GenricName);
SET @PackingKey     = (SELECT PackingKey     FROM PackingMaster             WHERE PackingName = @PackingName);
SET @ManufactureKey = (SELECT ManufactureKey FROM ManufactureMaster         WHERE ManufactureName = @ManufactureName);
SET @ShelfKey       = (SELECT ShelfKey       FROM ShelfMaster               WHERE ShelfName = @ShelfName);
SET @TherapeuticKey = (SELECT TherapeuticKey FROM TherapeuticCategoryMaster WHERE TherapeuticName = @TherapeuticName);


It is a little more verbose and clearer that an assignment is taking place, but this is just my personal preference.

Regards,

Manfred
 
Share this answer
 
v3
Comments
Maciej Los 15-May-12 4:53am    
Good answer, my 5!
Manfred Rudolf Bihy 15-May-12 7:04am    
Thanks!

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



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