Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2007, 08:15 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default A select and then an insert in a stored proc

Hi,

I'm trying to use a select statement to retrieve a value and then use this value in an insert. I've written the entire code inside a stored proc. Darn thing refuses to work. Please help
Here is a call to this stored proc
Set_NewUserName 'mm', 'mm', 'student', '[email protected]', 'mm', 'mm', 'mm', 'add1', 'add2', '600041', 'hybad', '93805', 'city', 'chennai', '13/10/1972', '6'

the code above sends third param 'student' to retrieve UserType_ID from tblUserType table. Also the last parameter is Class ID in the insert statement, for which I am passing the class value which will in turn retrieve the class ID from the tblClass table.

STORED PROCEDURE CODE BELOW HERE

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Set_NewUserName]
-- Following parameters will be sent to this proc by a webpage
-- We need to use the value in UserType_Name to identify if this is a
-- teacher or a student and insert into tblUser the appropriate value
-- similarly for class Id, retrive from class table using the value user
-- selects from drop down
@Login_ID nvarchar(20), @Password nvarchar(30), @UserType_Name nvarchar(30),
@Email nvarchar(30), @FirstName nvarchar(10), @LastName nvarchar(10),
@FullName nvarchar(20),
@add1 nvarchar(10), @add2 nvarchar(10), @pin nvarchar(10),
@city nvarchar(10), @phone nvarchar(10), @HintQuestion nvarchar(MAX),
@HintAnswer nvarchar(50), @DOB nvarchar(10), @Class_Name nvarchar(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @Class_ID uniqueidentifier
Declare @UserType_ID uniqueidentifier

-- retrieve the class id first from the tblClass Table
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRANSACTION
IF NOT EXISTS
(select Class_ID from tblClass where Class_Name = @Class_Name)
RAISERROR('select Class_ID from tblClass where Class_Name',11,1)

BEGIN
ROLLBACK TRANSACTION
RAISERROR('You must provide a valid Class ID',11,1)
END

-- retrieve the usertype id first from the UserType Table
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRANSACTION
IF NOT EXISTS
(select UserType_ID from tblUserType where UserType_Name = 'student')
RAISERROR('select UserType_ID from tblUserType where UserType_Name',11,1)

BEGIN
ROLLBACK TRANSACTION
RAISERROR('You must provide a valid Status ID',11,1)
RETURN
END

-- At last the Insert statements for the procedure
-- is created below here

insert into tblUser
values (
newid(),
@Login_ID, @Password,
@UserType_ID,
@Email,
@FirstName, @LastName, @FullName,
@add1, @add2, @pin, @city,
@phone,
'True',
@HintQuestion, @HintAnswer,
@DOB,
@Class_ID
)
END
 
Old July 20th, 2007, 10:47 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to assign the value you retrieve from the select to some variable:

   select @Class_ID = Class_ID from tblClass where Class_Name = @Class_Name


Why are you putting selects into transactions? If all you are doing is selecting data, what is there to roll back?

-Peter
 
Old July 21st, 2007, 03:20 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,

I was told by someone that if all I wanted was to do an insert that used two variables retrieved using select statements, it could be written far simpler. I was then given the following code, but it fails, and I havent been able to figure it out. Here she is:
-------------------------------------------------------------------
INSERT INTO tblUser (login, password, usertype_id, email, firstname, lastname, fullname, add1, add2, pin, city, phone, booleanField, HintQuestion, HintAnswer, DOB, ClassID)
SELECT @login, @password, ut.usertype_id, @email, @firstname, @lastname, fullname, @add1, @add2, @pin, @city, @phone, 'True', @HintQuestion, @HintAnswer, @DOB, c.Class_ID
INNER JOIN tblClass c on (Class_Name = @Class_Name)
INNER JOIN tblUserType ut on (UserType_Name = 'student')

The error message I get when I use this inside a stored proc is
Msg 156, Level 15, State 1, Procedure Set_NewUserName, Line 38
Incorrect syntax near the keyword 'INNER'.


 
Old July 21st, 2007, 07:31 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Missing FROM clause...

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling an insert stored proc from a select stored dzitam SQL Language 10 April 2nd, 2007 12:39 PM
efficiency qn:ado .AddNew VS INSERT in stored proc ak Classic ASP Databases 1 February 25th, 2004 10:08 AM
effieciency: ado .AddNew vs Insert in Stored proc ak SQL Server ASP 2 February 25th, 2004 09:20 AM
Passing variables into the SELECT of Stored Proc kerrj SQL Language 1 October 15th, 2003 06:00 AM
Passing variables into the SELECT of Stored Proc kerrj SQL Server 2000 0 October 14th, 2003 10:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.