Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
hai friends,
i want to add two table values into one temporary table and then return this table.this will using stored procedure.for example.
table 1 contain col1,col2
table 2 contain col1
i want this three column to be stored into new temporery table and this table return to .cs file.how to write storedprocedure for this task
thanks in advance
Posted

1 solution

A stored procedure returns a resultset. You could think of this resultset as a table, especially if you capture the values into a DataTable variable. Inside the stored procedure, however, you can join the data in any way you want. In your case, you could do a join on the two tables and return the combined results. You would need to know how to join (link up) the two tables though. Here is an example (pseudocode):

SQL
CREATE PROCEDURE dbo.sprocJoinedRecords
AS
BEGIN
   SET NOCOUNT ON;

   SELECT t1.col1, t1.col2, t2.col1
   FROM table1 t1
   INNER JOIN table2 t2 ON t1.id = t2.foreignId
END


This makes a lot of assumptions but I think it should give you the model you will need to follow.

Update
Based upon the information you gave in the comments, you want to join two tables that have no way of relating to each other. This can be done as long as you understand the results of doing so. In this case you have a BranchName in one table and the academic year information in the other. My assumption then would be that you want to see the academic year information displayed for each branch. This would be one area where it would actually be logical to join two unrelated tables together. It would look like this:
SQL
CREATE PROCEDURE dbo.sprocJoinedRecords
AS
BEGIN
   SET NOCOUNT ON;

   SELECT t1.branchname, t2.yearfrom, t2.yearto
   FROM branch t1, academic_year t2
END

This will give you a list of all of the rows from the academic_year table for each row in the branch table. This is called a CROSS JOIN (sometimes referred to as a Cartesian join because it produces a cartesian product). You can read more about CROSS JOINs here:

http://www.tutorialspoint.com/sql/sql-cartesian-joins.htm[^]
 
Share this answer
 
v2
Comments
baskaran chellasamy 26-Jul-12 14:41pm    
The table doesnt have any relationship at all. so i couldnt able to using join method.is any other way to this problem?
Tim Corey 26-Jul-12 14:44pm    
If the data does not have any relationship between tables, why are you attempting to join it together? You have to have some idea of how you want the two tables to relate, right? Why don't you list a few key columns and what you want to see as a result.
Arunprasath Natarajan 26-Jul-12 14:44pm    
Without relationship we cant join or match 2 tables
baskaran chellasamy 26-Jul-12 14:58pm    
explaination of my task
table 1:brach
column1:branchname
table 2: academic_year
column1:yearfrom
column 2;yearto
I want to combind these two table value and to view on the windows form page.
Tim Corey 26-Jul-12 15:07pm    
I've updated my answer based upon this new information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900