Solved

t-sql help - stored procedure call anther stored procedure

Posted on 2013-02-05
12
350 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now