Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am New in Store Procedure.
I have a Store Procedure to insert values from client at runtime and also from another table's ID column. My SP holds 3 Column:
SId
Name
Addess

Now How to insert from both area(User Input in ID and Name column and Address is From another table)?
Thanks in Advance.
Posted
Comments
Purna.N 30-Dec-13 1:19am    
can you provide the 2 table structure
SumitChandra 30-Dec-13 1:57am    
1st Master Table:Project_Header
[SId] [bigint] IDENTITY(1,1) NOT NULL,
[ProjectName] [varchar](100) NOT NULL,
[ProjectType] [varchar](50) NOT NULL,
[ProjectLocation] [varchar](50) NOT NULL


AND THE 2nd Table Where i want to insert:Money_Receipt
[SId] [bigint] NOT NULL,
[Name] [varchar](50) NOT NULL,
[Address] [varchar](100) NOT NULL

Now Sid will come from Project_Header Table
and Name and Address from user input through a Store Procedure

1 solution

SQL
INSERT INTO insertTable (x, y, z)
SELECT x, y, z
FROM a INNER JOIN b ON a.id = b.id


This format will work fine. Do the Sql with the join and add the first line at the top to make an insert of it.


SQL
create table t1
( 
  id int,
  val varchar(5),
  dt datetime
)


create table t2 
(
  id int,
  val varchar(5)  
)

insert into t2 values(1,'test')
insert into t2 values(2,'check')

insert into t1
select id, val, getdate() from t2

select * from t1


This will output:

1 test 2013-12-30 21:17:42.117
2 check 2013-12-30 21:17:42.117

with what ever the time is there. You can replace getdate() with your parameters, in other words, all you need to do, is add them to the select list and this will work as you asked.
 
Share this answer
 
v2
Comments
SumitChandra 30-Dec-13 5:07am    
Thanks for your reply,

I think you didn't get what i'm trying to say! i'm not looking for the simple join... but for a join two tables with a column (SId) and few column which value will be provided by user at runtime(Name , Address)...
Christian Graus 30-Dec-13 5:11am    
So put your parameters in the insert list.

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