We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Stored Procedure, Insert with subquery

Medium Priority
804 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

Comment
Watch Question

CERTIFIED EXPERT
Awarded 2008
Awarded 2008
Commented:
change it to this:

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
as chap has shown, you don't use VALUES in conjunction with a SELECT.  

no points please.

Author

Commented:
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!

Author

Commented:
superman fast :P
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.