Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 March 30th, 2007, 01:15 PM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calling an insert stored proc from a select stored

Ok,

I have a stored proc that selects some values from a table like so:



CREATE PROCEDURE usp_InsertThisIntoThat

AS

BEGIN

@someValue int,



Select values from table where someValue = something



Next I need to insert the value(s) I get back into a new table, so I am calling another stored proc as so:



EXEC usp_Insert 1,ValueFromAboveGoesHere





END

GO



How do I get the value from the select statement into the insert statment?

I would think that it would be east to create a varible and all that, but nothing I have tried has worked.

I tried setting a value = to the select statement, but that only insets the last record.



Thanks in advance.
 
Old March 30th, 2007, 01:22 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

This can be done in 1 stored procedure.

In your INSERT stored procedure do this:

CREATE PROCEDURE foo @someValue int as

INSERT INTO table(column, column, column)
SELECT column, column, column From [table] where someValue = @someValue

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 30th, 2007, 03:55 PM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did try and, and it works really well for one value, but if I have more than one value it seems to only insert still one value.



 
Old April 2nd, 2007, 02:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Can you post the code that you tried in getting this work? Only then we can point where you go wrong or understand what needs to be altered to get it work the correct way you wanted it to.

When you say... you do this from within the procedure...
Code:
INSERT INTO TABLENAME Select values from table where someValue = something
See the insert statement prefixed there in bold letters. That should be enough to accomplish what you are trying to, if it is right that we understood from your post.

cheers.

_________________________
- Vijay G
Strive for Perfection
 
Old April 2nd, 2007, 06:33 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

dzitam, if this query:

INSERT INTO table(column, column, column)
SELECT column, column, column From [table] where someValue = @someValue

is only inserting one value, it is (probably) becuase of your WHERE statement.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 2nd, 2007, 10:45 AM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow, I can't believe this isn't working at this point.

I am getting some lookup values from a table:

1 1
1 2
1 3
2 1
1 4

where value = 1

I am getting from my select all of the values.
Now, for each value I get back, I need to insert into a new table a new row.
here is the code I am using based on how I think it should be from the above listing:


INSERT INTO Emission(SID, GID, IsActive) values( 113,

SELECT LookupGas.TypeID
FROM LookupEmissionSubCategory INNER JOIN
                      GasEmissionSubCategory ON LookupEmissionSubCategory.TypeID = GasEmissionSubCategory.EmissionSubCategoryID RIGHT OUTER JOIN
                      LookupGas ON GasEmissionSubCategory.GasCategoryID = LookupGas.GasCategoryID
WHERE (LookupEmissionSubCategory.TypeID = 4), 0)



Thanks for all your help:)

 
Old April 2nd, 2007, 11:57 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

hmmm that should give you an error.

INSERT INTO Emission(SID, GID, IsActive)
SELECT 113, LookupGas.TypeID, 0
FROM LookupEmissionSubCategory INNER JOIN
                      GasEmissionSubCategory ON LookupEmissionSubCategory.TypeID = GasEmissionSubCategory.EmissionSubCategoryID RIGHT OUTER JOIN
                      LookupGas ON GasEmissionSubCategory.GasCategoryID = LookupGas.GasCategoryID
WHERE (LookupEmissionSubCategory.TypeID = 4)

and, based on your example, Where value = 4 should only return 1 row.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 2nd, 2007, 12:15 PM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oh, you're right, my bad.
Suposing we change the value from 4 to 1, any insight on how to make it work, or are you just pointing out that 4 would return one value?


 
Old April 2nd, 2007, 12:24 PM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oh, you're right, my bad.
Suposing we change the value from 4 to 1, any insight on how to make it work, or are you just pointing out that 4 would return one value?


Update, oh wait, I see more now that I look:)
Let me try you're posted code.



 
Old April 2nd, 2007, 12:31 PM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am so happy to say that you are the one:)
I would have never gotten this without you, thanks a lot,







Similar Threads
Thread Thread Starter Forum Replies Last Post
A select and then an insert in a stored proc smacks SQL Server 2005 3 July 21st, 2007 07:31 PM
Calling Stored Proc with OUT params nkrust ASP.NET 2.0 Professional 2 April 19th, 2007 12:31 AM
Calling a T-SQL Stored proc from DTS mepancha SQL Server 2000 1 April 13th, 2005 09:12 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.