I've created a insert stored procedure with two tables like in the exapmle:
reference table :
create table customerdetails (eid int,
dsgid int Foreign Key Referencesdesg_d(d_id),
ename varchar(90),
dob datetime,addr varchar(100),pincode int,salary,int,dojdatetime)
insert into customerdetails values (1,1,'ahalyaa','05.13.1993','chennai',600024,345,'06.02.2014')
source table:
create table desg_d(d_id int primary key,desg varchar(90))
insert into desg_d values(1,'programmer')
insert into desg_d values(2,'manager')
my store procedure:
create procedure sp_i #iid int,#iname varchar(90),#idobdatetime,
#iaddr varchar(100),#ipincode int, #isalary int,#iDoj datetime
as
begin
declare #idesg int
set #idesg=1
insert into customerdetails(eid,dsgid,ename,dob,addr,pincode,salary,doj)
values(#iid,#idesg,#iname,#idob,#iaddr,#ipincode,#isalary,#iDoj)
end
if i give set=1,then always idesg value should be 1, but i need to insert idesg value randomly, pls help me.
Foreign key values are also the same with the normal insert. The difference is that foreign key values to be inserted should exist on the main table.
Also, please reconsider on naming your variable in your stored procedure. Please see sample below.
create procedure sp_i
#eid int
,#dsgid int
,#ename varchar(90)
,#dob datetime
,#addr varchar(100)
,#pincode int
, #salary int
,#Doj datetime
as
begin
declare #idesg int
insert into customerdetails
(eid,dsgid,ename,dob,addr,pincode,salary,doj)
values
(#eid,#dsgid,#ename,#dob,#addr,#pincode,#salary,#Doj)
end
Use following format in your stored procedure:
DECLARE #DesgId int
INSERT INTO Desg(COLUMN) VALUES(#VALUES)
SET #DesgId = SCOPE_IDENTITY()
INSERT INTO customerdetails ( ..., Dsgid, ...)
VALUES (..., #DesgId, ...)
You can also use following format:
INSERT INTO Desg(COLUMN) VALUES(#VALUES)
INSERT INTO customerdetails ( ..., Dsgid, ...)
VALUES (..., (Select Top(1) d_id from desg_d where desg = #Desg), ...)
How do you insert the results of a dynamic SQL server query and a variable into the same row of a temp table.
Here is the format of the temporary table (#temp)
create TABLE #temp (YEAR INT, ACC_AMOUNT Money, REJ_AMOUNT Money, OUT_AMOUNT Money,TOT_AMOUNT Money)
The Stored PROC is executed by
exec sp_executesql #pSQL
and will return values that should go into the columns ACC_AMOUNT, REJ_AMOUNT, OUT_AMOUNT.
14569849.11 696235.49 1353464.92 16619549.52
I want to insert a variable (#pACCU_YEAR) for the Year and the results of the SP and insert the dyanmic query in the same Row. This is wrong, but this will give you an idea of what I mean.
insert into #temp (YEAR)
values (#pACCU_YEAR)
insert into #temp (ACC_AMOUNT , REJ_AMOUNT, OUT_AMOUNT,TOT_AMOUNT)
exec sp_executesql #pSQL
This will result in
YEAR ACC_AMOUNT REJ_AMOUNT OUT_AMOUNT TOT_AMOUNT
2014 NULL NULL NULL NULL
NULL 14569849.11 696235.49 1353464.92 16619549.52
I would like:
YEAR ACC_AMOUNT REJ_AMOUNT OUT_AMOUNT TOT_AMOUNT
2014 14569849.11 696235.49 1353464.92 16619549.52
Select #Year [Year]
,Acc_Amount
,other fields....
FROM #Temp
In response to the question asked in your comment.
Use this after the #pSql is run. Leave the column Year out of the declaration.
Alternatively, after it runs just:
update #temp
SET [Year]=#Year
What's the code inside #pSQL? Why don't do the INSERT in your dynamic query itself like
declare #pSQL varchar(max);
set #pSQL = 'some sql command;
some_other_sql_command;
.......
insert into #temp (ACC_AMOUNT , REJ_AMOUNT, OUT_AMOUNT,TOT_AMOUNT)
select blah,blah1,blah2,blah3
from blah_table;';
exec sp_executesql #pSQL;
can you please show me an example of how to select a column that is a
variable not within the table?
use same SELECT statement like
select #var1 as some_name, #var2 as some_other_name
as some_name is optional. That's to give a meaningful name to the selected field.
i am exporting some date to sql server using string builder.and here i assign all the values to particular tables.but i don't know i got some error.i can't resolve.help me to resolve this.
ALTER PROCEDURE [dbo].[usp_CreateEmployeDetails]
#nEmployeeDetailsInXML nvarchar(max)=''
As
DECLARE #iTree INTEGER
declare #empid int
BEGIN
SET NOCOUNT ON;
create table #TempRelation(RowNo int identity(1,1),RelationShipId int,RelativeName nvarchar(100),DOB date,IsNominee bit)
create table #Temp_Present_Address(RowNo int identity(1,1),Street1 nvarchar(200),Street2 nvarchar(200),CountryId int,StateId int,CityId int,AddressTypeId int)
create table #Temp_Permanent_Address(RowNo int identity(1,1),Street1 nvarchar(200),Street2 nvarchar(200),CountryId int,StateId int,CityId int,AddressTypeId int)
create table #TempDetails(RowNo int identity(1,1),EmployeeName nvarchar(100),DOB date,DOJ date,Email nvarchar(100),Phone bigint,BoodGroup nchar(10),
PAN_No nvarchar(15),PF_No nvarchar(100),Sex char(10),AccountNo nvarchar(100),BankName nvarchar(100),BranchId int,ManagerId int,HrId int,DesigId int)
exec sp_xml_preparedocument #iTree output,#nEmployeeDetailsInXML
insert into #TempRelation(RelationShipId,RelativeName,DOB,IsNominee)
select RelationShipId,RelativeName,DOB,IsNominee
from openxml (#iTree,'EmployeeDetails/EmployeeRelation',1)
with(RelationShipId int,RelativeName nvarchar(100),DOB date,IsNominee bit)
insert into #Temp_Permanent_Address(Street1,Street2,CountryId,StateId,CityId,AddressTypeId)
select Street1,Street2,CountryId,StateId,CityId,AddressTypeId
from openxml (#iTree,'EmployeeDetails/EmployeePermanentAdress',1)
with(Street1 nvarchar(200),Street2 nvarchar(200),CountryId int,StateId int,CityId int,AddressTypeId int)
insert into #Temp_Present_Address(Street1,Street2,CountryId,StateId,CityId,AddressTypeId)
select Street1,Street2,CountryId,StateId,CityId,AddressTypeId
from openxml (#iTree,'EmployeeDetails/EmployeePresentAdress',1)
with(Street1 nvarchar(200),Street2 nvarchar(200),CountryId int,StateId int,CityId int,AddressTypeId int)
insert into #TempDetails(EmployeeName,DOB,DOJ,Email,Phone,BoodGroup,PAN_No,PF_No,Sex,AccountNo,BankName,BranchId,ManagerId,HrId,DesigId)
select EmployeeName,DOB,DOJ,Email,Phone,BoodGroup,PAN_No,PF_No,Sex,AccountNo,BankName,BranchId,ManagerId,HrId,DesigId
from openxml (#iTree,'EmployeeDetails/Employee',1)
with(EmployeeName nvarchar(100),DOB date,DOJ date,Email nvarchar(100),Phone bigint,BoodGroup nchar(10),PAN_No nvarchar(15),PF_No nvarchar(100),Sex char(10),
AccountNo nvarchar(100),BankName nvarchar(100),BranchId int,ManagerId int,HrId int,DesigId int)
if((select COUNT(RowNo) from #TempDetails)>0)
begin
insert into Employee(EmployeeName,DOB,DOJ,Email,Phone,BoodGroup,PAN_No,PF_No,Sex)output inserted.EmployeeId select EmployeeName,DOB,DOJ,Email,Phone,BoodGroup,PAN_No,PF_No,Sex from #TempDetails
set #empid=SCOPE_IDENTITY()
if((select COUNT(EmployeeName) from Employee where EmployeeId=#empid)>0)
begin
insert into EmployeeAccountDetls(EmployeeId,AccountNo,BankName)values(#empid,(select AccountNo,BankName from #TempDetails))
insert into EmployeeLink(EmployeeId,BranchId,ManagerId,HrId,DesigId)values(#empid,(select BranchId,ManagerId,HrId,DesigId from #TempDetails))
if((select COUNT(RowNo) from #Temp_Permanent_Address)>0)
begin
insert into EmployeeAddress(EmployeeId,Street1,Street2,CountryId,StateId,CityId,AddressTypeId)values(#empid,(select Street1,Street2,CountryId,StateId,CityId,AddressTypeId from #Temp_Permanent_Address))
end
if((select COUNT(RowNo) from #Temp_Present_Address)>0)
begin
insert into EmployeeAddress(EmployeeId,Street1,Street2,CountryId,StateId,CityId,AddressTypeId)values(#empid,(select Street1,Street2,CountryId,StateId,CityId,AddressTypeId from #Temp_Present_Address))
end
if((select COUNT(RowNo) from #TempRelation)>0)
begin
insert into EmployeeRelationDetls(EmployeeId,RelationShipId,RelativeName,DOB,isNominee)values(#empid,(select RelationShipId,RelativeName,DOB,IsNominee from #TempRelation))
end
end
end
EXEC sp_xml_removedocument #iTree
drop table #Temp_Present_Address
drop table #Temp_Permanent_Address
drop table #TempDetails
drop table #TempRelation
END
why its happening i checked but i didn't get the result
For the specific error you mention, your problem is here:
insert into EmployeeAddress(EmployeeId,Street1,Street2,CountryId,StateId,CityId,AddressTypeId)
values (#empid, (select Street1,Street2,CountryId,StateId,CityId,AddressTypeId from #Temp_Permanent_Address))
You probably want:
insert into EmployeeAddress(EmployeeId,Street1,Street2,CountryId,StateId,CityId,AddressTypeId)
SELECT #empid,Street1,Street2,CountryId,StateId,CityId,AddressTypeId from #Temp_Permanent_Address
However, you're inserting multiple rows and then giving the last rows IDENTITY value to every address row doing that. You need to find a better way of relating keys - which can either be done using MERGE's output clause or by using a stronger key to reference back to the table you just inserted into in order to find the newly inserted employee ID.