I need to create a temporary table and populate it with temporary values. The variables have values assigned from a python script. My code is as follows:
ALTER PROCEDURE [dbo].[AddScrapedInfoBULK]
(
-- Parameters for the SP, (each field in the all tables)
-- ProjectInfo Fields
#ProjectInfoID AS INT,
#OrderNumber AS NVARCHAR(255),
#PeriodofPerformance AS NVARCHAR(255),
#POPEndDate AS DATETIME,
#PopStartDate AS DATETIME
AS
BEGIN
SET NOCOUNT ON;
DECLARE #temproj TABLE (ProjectInfoID INT,
OrderNumber NVARCHAR(255),
PeriodofPerformance NVARCHAR(255),
POPEndDate DATETIME,
PopStartDate DATETIME)
INSERT INTO #temproj
SELECT (#ProjectInfoID,
#OrderNumber,
#PeriodofPerformance,
#POPEndDate,
#PopStartDate)
END
but this does not work. How can I populate a temporary table with variables?
You can just use insert into .... values to make it.
INSERT INTO #temproj
(projectinfoid,
ordernumber,
periodofperformance,
popenddate,
popstartdate)
VALUES (#ProjectInfoID,
#OrderNumber,
#PeriodofPerformance,
#POPEndDate,
#PopStartDate)
Remove the parentheses around the select.
DECLARE #ProjectInfoID AS INT,
#OrderNumber AS NVARCHAR(255),
#PeriodofPerformance AS NVARCHAR(255),
#POPEndDate AS DATETIME,
#PopStartDate AS DATETIME
SET NOCOUNT ON;
DECLARE #temproj TABLE (ProjectInfoID INT,
OrderNumber NVARCHAR(255),
PeriodofPerformance NVARCHAR(255),
POPEndDate DATETIME,
PopStartDate DATETIME)
INSERT INTO #temproj
SELECT #ProjectInfoID,
#OrderNumber,
#PeriodofPerformance,
#POPEndDate,
#PopStartDate
May I ask a question regarding for the SQL Server stored procedure?
Basically I created a table called Customers and I want to write an INSERT statement inside of the stored procedure where I have been done using CURSOR.
When I execute my CREATE stored procedure should be fine and do not have any error message and also when I execute the stored procedure using EXECUTE statement without any data entry (7, Andy, Singapore) do not have any error message.
Lastly, when I put some data entry into the EXECUTE stored procedure and I got this error messages.
Msg 8146, Level 16, State 1, Procedure InsertCustomers_Cursor, Line 0 [Batch Start Line 40]
Procedure InsertCustomers_Cursor has no parameters and arguments were supplied.
Code:
create table customer
(
cust_id int primary key,
name varchar(100),
country varchar(50)
);
insert into customer
values (1, 'John Hammond', 'United States'),
(2, 'Mudassar Khan', 'India'),
(3, 'Robert Tan', 'Singapore'),
(4, 'Dennis Rodman', 'Indonesia'),
(5, 'Michelle Chia', 'Indonesia'),
(6, null, null);
select *
from customer;
CREATE PROCEDURE InsertCustomers_Cursor
AS
BEGIN
DECLARE #cust_id int,
#name varchar(100),
#country varchar(50)
DECLARE #Counter INT
SET #Counter = 1
DECLARE #AllRecords TABLE
(
cust_id int,
name varchar(100),
country varchar(50)
)
DECLARE InsertCustomers CURSOR READ_ONLY
FOR
SELECT cust_id, name, country
FROM customer
OPEN InsertCustomers
FETCH NEXT FROM InsertCustomers INTO #cust_id, #name, #country
WHILE ##FETCH_STATUS = 0
BEGIN
INSERT INTO #AllRecords
VALUES (#cust_id, #name, #country)
FETCH NEXT FROM InsertCustomers INTO #cust_id, #name, #country
END
CLOSE InsertCustomers
DEALLOCATE InsertCustomers
END
GO
-- This EXEC statement cannot run the query
EXEC InsertCustomers_Cursor 7, 'Michael Labone', Indonesia
-- This EXEC statement can run the query and the results is to show how many are being affected
EXEC InsertCustomers
The issue is your SP doesn't expect any parameter and you are calling it passing parameters
EXEC InsertCustomers_Cursor 7, 'Michael Labone', Indonesia
To make it works properly, you need to create these parameters in the SP:
CREATE PROCEDURE InsertCustomers_Cursor
#cust_id int,
#name varchar(100),
#country varchar(50)
AS
BEGIN
...
And remove this part:
DECLARE #cust_id int,
#name varchar(100),
#country varchar(50)
There could be more elegant ways to declare these parameters but for the example I think it's enough.
I'm working on a cascading insertion where a stored procedure should return an id of the inserted row. This would not be a problem if the id of the table was an int. However, the id is a varchar and therefore I cannot use SCOPE_IDENTITY().
This is my procedure so far:
CREATE PROCEDURE NEW_ARTICLE
#id varchar(50) OUTPUT,
#name varchar(100),
#articleNr varchar(50),
#gategory varchar(50),
#containerId varchar(50),
#contPerContainer int,
#pictureId varchar(50)
AS
SET NOCOUNT OFF
INSERT INTO nextlabel.[Article] (name, article_nr, category, container_id, count_per_container, picture_id )
VALUES (#name, #articleNr, #gategory, #containerId, #contPerContainer, #pictureId)
SET #id = SCOPE_IDENTITY()
Where the last row is not correct since the column id is a varchar.
How can I return the id?
Try this:
CREATE PROCEDURE NEW_ARTICLE
#id varchar(50) OUTPUT,
#name varchar(100),
#articleNr varchar(50),
#gategory varchar(50),
#containerId varchar(50),
#contPerContainer int,
#pictureId varchar(50)
AS
SET NOCOUNT OFF
SET #id = newid()
INSERT INTO nextlabel.[Article] (id, name, article_nr, category, container_id, count_per_container, picture_id)
VALUES (#id, #name, #articleNr, #gategory, #containerId, #contPerContainer, #pictureId)
GO
I want to transfer data from source Database table named Patient (which contains many rows) to destination database tables(2) named Person & Patient.
I already have stored procedure named AddPatient in destination database which will add person related fields to Person table and other fields to Patient table, so I would like to execute that procedure and to assign the fields from source database as variables to it. The following are the code of AddPatient sp in destination database.
ALTER PROCEDURE [dbo].[AddPatient]
(
#TenantId BIGINT,
#FirstName NVARCHAR(100),
#LastName NVARCHAR(100),
#PersonNumber NVARCHAR(20),
#MobileNumber NVARCHAR(20),
#EmailId NVARCHAR(100),
#Address NVARCHAR(255),
#City NVARCHAR(50),
#ZipCode NVARCHAR(20),
#ListComments NVARCHAR(1000),
#Comment NVARCHAR(500),
#AlternateEmailId NVARCHAR(100) ,
#HomePhone NVARCHAR(20) ,
#Relative NVARCHAR(255) ,
#HasDiabetes [bit],
#HasBlooPressure [bit],
#AddedBy BIGINT,
#AddedDateTime smalldatetime,
#PersonId BIGINT OUTPUT
)
AS
BEGIN
SET NOCOUNT ON
IF #TenantId IS NULL
RAISERROR('The value for #TenantID should not be null', 15, 1) -- with log
ELSE
BEGIN
DECLARE #new_person_id BIGINT
DECLARE #new_patient_id BIGINT
DECLARE #PatientIdentifier NVARCHAR(50)
EXEC dbo.GetNextPatientIdForTenant #TenantID, #PatientIdentifier OUTPUT
INSERT INTO dbo.Person
(
TenantId,
FirstName,
LastName,
PersonNumber,
MobileNumber,
EmailId,
Address,
City,
ZipCode,
AddedBy,
AddedDateTime
)
VALUES
(
#TenantId,
#FirstName,
#LastName,
#PersonNumber,
#MobileNumber,
#EmailId,
#Address,
#City,
#ZipCode,
#AddedBy,
#AddedDateTime
)
SELECT #new_person_id = SCOPE_IDENTITY()
INSERT INTO dbo.Patient
(
TenantId,
PatientIdentifier,
PersonId,
ListComments,
Comment,
AlternateEmailId,
HomePhone,
Relative,
HasDiabetes,
HasBlooPressure,
AddedBy,
AddedDateTime
)
VALUES
(
#TenantId,
#PatientIdentifier,
#new_person_id,
#ListComments ,
#Comment ,
#AlternateEmailId,
#HomePhone ,
#Relative ,
#HasDiabetes,
#HasBlooPressure,
#AddedBy ,
#AddedDateTime
)
SELECT #new_patient_id = SCOPE_IDENTITY()
SELECT #PersonId = #new_person_id
SELECT #new_patient_id
END
END
There is no TenantId & AddedBy field in source, so I want to assign both as 1 for all rows to be transfered.
I know Execute SQL Task will handles stored procedure and for each row data Foreach Loop Container will take care in SSIS. But I don't know how to assign the variables of sp in destination database to the fields of table from source database.
Anyone help me with this.
Thanks in advance !
I am created a stored procedure in SQL Server and I'm getting this error but, I'm not sure what it means.
The following is the procedure:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Grant_Append]
-- Add the parameters for the stored procedure here
(#GrantNumber AS VARCHAR(50),
#GrantName AS VARCHAR(100),
#ProjectCode AS VARCHAR(50),
#LOCID AS INT,
#Comments AS VARCHAR(100),
#Provider AS VARCHAR(100),
#ISAT AS NCHAR(10),
#TypeID AS INT,
#Slots AS FLOAT,
#Patients AS FLOAT,
#FundingMethod AS VARCHAR(50),
#L1600 AS MONEY,
#L1602 AS MONEY,
#L1603 AS MONEY,
#L1608 AS MONEY,
#L1612 AS MONEY,
#ADAAFunding AS MONEY,
#ProgramBudget AS MONEY,
#Jurisdiction AS INT,
#VersionType AS VARCHAR(20),
#VersionNum AS INT)
AS BEGIN
INSERT INTO dbo.Grant
(GrantNumber,
GrantName,
ProjectCode,
LOCID,
Comments,
Provider,
ISAT,
TypeID,
Slots,
Patients,
FundingMethod,
[1600],
[1602],
[1603],
[1608],
[1612],
ADAAFunding,
ProgramBudget,
Jurisdiction,
VersionType,
VersionNum,
SubmitDate)
-- Insert statements for procedure here
SELECT #GrantNumber,
#GrantName,
#ProjectCode,
#LOCID,
#Comments,
#Provider,
#ISAT,
#TypeID,
#Slots,
#Patients,
#FundingMethod,
#1600,
#1602,
#1603,
#1608,
#1612,
#ADAAFunding,
#ProgramBudget,
#Jurisdiction,
#VersionType,
#VersionNum,
getdate()
END
These are the errors:
Msg 156, Level 15, State 1, Procedure Grant_Append, Line 30
Incorrect syntax near the keyword 'Grant'.
Msg 137, Level 15, State 2, Procedure Grant_Append, Line 65
Must declare the variable '#1600'
GRANT is a Reserved keyword. It should be delimited to avoid error, eg
INSERT INTO dbo.[Grant] ....
SQL Server Delimited Identifiers
SQL Server Reserved Keywords
there are also typos in your parameters,
#1600,
#1602,
#1603,
#1608,
#1612,
when it should be
#L1600,
#L1602,
#L1603,
#L1608,
#L1612,
I'd recommend changing your table name from [Grant], which is a reserved keyword, to something not reserved, i.e. [Grants]. Also, I'd suggest not beginning column names with numbers. So, for instance, prefix [1600] with something descriptive. I've used "fee" as an example. Here's a working code sample for you:
-- DROP TABLE dbo.Grants
CREATE TABLE dbo.Grants
(
GrantNumber VARCHAR(50),
GrantName VARCHAR(100),
ProjectCode VARCHAR(50),
LOCID INT,
Comments VARCHAR(100),
Provider VARCHAR(100),
ISAT NCHAR(10),
TypeID INT,
Slots FLOAT,
Patients FLOAT,
FundingMethod VARCHAR(50),
Fee1600 MONEY,
Fee1602 MONEY,
Fee1603 MONEY,
Fee1608 MONEY,
Fee1612 MONEY,
ADAAFunding MONEY,
ProgramBudget MONEY,
Jurisdiction INT,
VersionType VARCHAR(20),
VersionNum INT,
SubmitDate DATETIME
);
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GrantsInsert_sp]
-- Add the parameters for the stored procedure here
(#GrantNumber AS VARCHAR(50),
#GrantName AS VARCHAR(100),
#ProjectCode AS VARCHAR(50),
#LOCID AS INT,
#Comments AS VARCHAR(100),
#Provider AS VARCHAR(100),
#ISAT AS NCHAR(10),
#TypeID AS INT,
#Slots AS FLOAT,
#Patients AS FLOAT,
#FundingMethod AS VARCHAR(50),
#Fee1600 AS MONEY,
#Fee1602 AS MONEY,
#Fee1603 AS MONEY,
#Fee1608 AS MONEY,
#Fee1612 AS MONEY,
#ADAAFunding AS MONEY,
#ProgramBudget AS MONEY,
#Jurisdiction AS INT,
#VersionType AS VARCHAR(20),
#VersionNum AS INT)
AS BEGIN
INSERT INTO dbo.Grants
(GrantNumber,
GrantName,
ProjectCode,
LOCID,
Comments,
Provider,
ISAT,
TypeID,
Slots,
Patients,
FundingMethod,
Fee1600,
Fee1602,
Fee1603,
Fee1608,
Fee1612,
ADAAFunding,
ProgramBudget,
Jurisdiction,
VersionType,
VersionNum,
SubmitDate)
-- Insert statements for procedure here
SELECT #GrantNumber,
#GrantName,
#ProjectCode,
#LOCID,
#Comments,
#Provider,
#ISAT,
#TypeID,
#Slots,
#Patients,
#FundingMethod,
#Fee1600,
#Fee1602,
#Fee1603,
#Fee1608,
#Fee1612,
#ADAAFunding,
#ProgramBudget,
#Jurisdiction,
#VersionType,
#VersionNum,
GETDATE();
END;
EXECUTE [GrantsInsert_sp] 'abc', 'abc', 'abc', 1, 'abc', 'abc', 'abc', 1, 1, 1, 'abc', 1, 1, 1, 1, 1, 1, 1, 1, 'abc', 1;
SELECT * FROM dbo.Grants;