Solved

t-sql help - stored procedure call anther stored procedure

Posted on 2013-02-05
12
354 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

773 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