Solved

t-sql help - stored procedure call anther stored procedure

Posted on 2013-02-05
12
357 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 500 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

680 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