Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

t-sql help - stored procedure call anther stored procedure

Posted on 2013-02-05
12
Medium Priority
?
367 Views
Last Modified: 2013-02-06
I need help on #2 and #3:

1) select data from a table, stored the STD_ID in the temp table.

2) need to execute anther stored procedure 'GET_STD_INFO ' pass in the STD_ID, it will return  columns included the following:
STD_ID, STD_NAME, ACTIVE_IND AND CLASSES_TOTAL, etc...

3) need to update the temp table with the return columns of STD_ID, STD_NAME, ACTIVE_IND AND CLASSES_TOTAL



DECLARE @TMP_TEST table
(
      STD_ID                  INT,
      STD_NAME            VARCHAR(50),
      ACTIVE_IND            CHAR(1),
      CLASSES_TOTAL            INT
      )



INSERT @TMP_TEST
      (
      STD_ID
      )
SELECT  DISTINCT
      TOP 1 STD_ID
FROM STUDENT_TB
ORDER BY ACTIVE_YEAR


what is the best way to do this?

thank you,

I am running in  MS SQL server 2010.
0
Comment
Question by:jfreeman2010
  • 7
  • 4
12 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38857173
seem like an assignment to me ?
0
 

Author Comment

by:jfreeman2010
ID: 38857241
Its better way to do this beside to DECLARE CURSOR on #2?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38857250
yes, you can do that withou a cursor. but we are not suppose to provide answers to the assignments
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:jfreeman2010
ID: 38857276
Hi aneeshattingal,

I don't understand what you mean assignments????
0
 

Author Comment

by:jfreeman2010
ID: 38857476
this is NOT a student assignment, the example above just to make it easy to understand ( at lease if for me), and it part of what I need to do for work.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 38857487
the way you described the question seems suspecious. thats why I asked you


declare @rows int , @stdid int 


DECLARE @TMP_TEST table
(     i int identity , 
      STD_ID                  INT,
      STD_NAME            VARCHAR(50),
      ACTIVE_IND            CHAR(1),
      CLASSES_TOTAL            INT
      ) 

CREATE TABLE #tmp
(    
      STD_ID                  INT,
      STD_NAME            VARCHAR(50),
      ACTIVE_IND            CHAR(1),
      CLASSES_TOTAL            INT
      ) 

INSERT @TMP_TEST
      (
      STD_ID
      )
SELECT  DISTINCT
      TOP 1 STD_ID
FROM STUDENT_TB 

select @rows = @@ROWCOUNT

while @rows > 0 
BEGIN
	SELECT @stdid =  std_id 
	from @TMP_TEST 
	where i = @rows 
	
	SELECT @rows = @rows - 1 
	insert into #tmp
	exec GET_STD_INFO @stdid 
END 

update t
set std_name = d.std_name, 
    active_ind = d.active_ind,
    Classes_total = d.classes_total
FROM @TMP_TEST t
inner join #temp d on t.STD_ID = d.std_id

Open in new window

0
 

Author Comment

by:jfreeman2010
ID: 38857553
Hi aneeshattingal,

THANK YOU FOR HELP!!!  I think will just make it easy to ask the question.

I have 2 questions :  

1) can the #tmp be @tmp as table variable ?
2) what happen is the get_std_info return more columns then the #tmp/@tmp table list columns?  do I need to list all the columns return from get_std_info in the #tmp/@tmp?



Thank you very much for help.
0
 

Expert Comment

by:callmeraghavendra
ID: 38859767
Hi,

yes the #temp table can be used as @temp variable and you might need to tweak the get_std_info to return only the columns that you like to insert into the Temp table.
0
 

Author Comment

by:jfreeman2010
ID: 38859787
ok, I will try.  thank you very much for your helping...
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 2000 total points
ID: 38860053
>what happen is the get_std_info return more columns then the #tmp/@tmp table list columns?  do I need to list all the columns return from get_std_info in the #tmp/@tmp?
I would take the logic of get_std_info and join the @TMP_TEST  table, rather than loop thru each id and call the sp, but it depends on the complexity of your stored procedure; Other option will be conver get_std_info  to a function and join with @TMP_TEST  table.

Aneesh
0
 

Author Comment

by:jfreeman2010
ID: 38860119
YES Aneesh, that is what I am trying to do, and the logic is very  complex and I am still working.... thank you for your suggestion.
0
 

Author Closing Comment

by:jfreeman2010
ID: 38860129
thank you for all your help!!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

824 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