Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Stored Procedure, Insert with subquery

Posted on 2009-02-17
4
Medium Priority
?
786 Views
Last Modified: 2012-05-06
I want to create a stored procedure that uses a subquery to insert the max Identity from a table into another table (last record added)  
I wrote the code but it says that sub queries are not allowed in the insert statements, only scalar expressions.  If its not possible, how do I use the same select statement to save the return to a scalar value?
CREATE PROCEDURE NewCaseInsertDocs 
	-- Add the parameters for the stored procedure here
	@MaxCaseId int
 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
    -- Insert statements for procedure here
  	INSERT INTO dbo.Documents_Needed 
	                      (CaseID, Type)
	VALUES     ((SELECT MAX(CaseID) From dbo.ACF_Cases),'Assign')
END
GO

Open in new window

0
Comment
Question by:HarleySkater
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 23663526
change it to this:

INSERT INTO dbo.Documents_Needed
                            (CaseID, Type)
      SELECT MAX(CaseID), 'Assign' From dbo.ACF_Cases
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 23663581
as chap has shown, you don't use VALUES in conjunction with a SELECT.  

no points please.
0
 
LVL 1

Author Comment

by:HarleySkater
ID: 23663626
Im awarding all the points to Brandon :P J/k
 
Thanks works like a charm.
Is there a way to set the scalar value? to the select statement.  I know its 2 statements instead of 1 but its still good information to know
 
Thank yoU!
0
 
LVL 1

Author Closing Comment

by:HarleySkater
ID: 31547969
superman fast :P
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question