I have two servers. I copied the table "Response_Master_Incident" on cadarchive server and duplicated it into the ucpdapps2 server and named it "Master_Incident_For_ProQA". When I duplicated it I only selected certain columns to duplicate (since I didn't need all the columns from "Response_Master_Incident").
Now I am trying to create a stored procedure to update the data from "Response_Master_Incident" to "Master_Incident_For_ProQA" pulling over only those select columns.
create procedure UpdateProQATable
as
begin
Select [ID]
,[Master_Incident_Number]
,[Response_Date]
,[Problem]
,[MethodOfCallRcvd]
,[CallTaking_Performed_By]
,[EMD_Used]
,[Determinant]
,[ProQa_CaseNumber]
,[ProQa_CaseNumber_Fire]
,[ProQa_CaseNumber_Police]
,[MachineName]
into Master_Incident_For_ProQA
from Response_Master_Incident where EMD_Used = '1'
end
When I run this stored procedure I get this error
"Msg 208, Level 16, State 1, Procedure UpdateProQATable, Line 4 [Batch Start Line 2]
Invalid object name 'Response_Master_Incident'."
How do I resolve this error. And is there a way to have the procedure update the table where the "Response_Date" is a date from yesterday and not all the data from the "Response_Master_Incident" table?
So I figured it out. I needed to go to Databases/Server Objects/Linked Servers and link the server that has the data I needed to pull over to the server that was getting the data.
After I did that I corrected my script as suggested in the above comment but also added script to delete all the data currently in the table, insert new data, and filtered the new data by the column of EMD_Used and the Response_Date as yesterdays date. After doing this the desired result was met.
delete from Master_Incident_For_ProQA
insert into [ucpdapps2].[ProQAUsage].[dbo].[Master_Incident_For_ProQA]
([ID]
,[Master_Incident_Number]
,[Response_Date]
,[Problem]
,[MethodOfCallRcvd]
,[CallTaking_Performed_By]
,[EMD_Used]
,[Determinant]
,[ProQa_CaseNumber]
,[ProQa_CaseNumber_Fire]
,[ProQa_CaseNumber_Police]
,[MachineName])
Select [ID]
,[Master_Incident_Number]
,[Response_Date]
,[Problem]
,[MethodOfCallRcvd]
,[CallTaking_Performed_By]
,[EMD_Used]
,[Determinant]
,[ProQa_CaseNumber]
,[ProQa_CaseNumber_Fire]
,[ProQa_CaseNumber_Police]
,[MachineName] FROM [cadarchive].[Reporting_System].[dbo].[Response_Master_Incident] where [EMD_Used] = 1 and [Response_Date] >= DATEADD(day, -1, CONVERT(date, getdate())) AND
[Response_Date] < CONVERT(date, getdate())
Related
I have an existing table that resides in one database, and each night I want to completely overwrite all contents of this existing table (TO_TABLE), with all data from another database/table (FROM_TABLE).
Currently I am manually dropping the TO_TABLE and re-writing it using this:
INTO SDE_SPATIAL.GISADMIN.TO_TABLE
FROM GAVIN..AUTH.FROM_TABLE
This works fine, but I would eventually like to convert this into a stored procedure and get this to happen automatically every 24hrs.
Does anyone know how to do the above a bit better and ready for a stored procedure??
UPDATE:
This is the code as it currently stands. I was getting these two errors occur when I run it, the first was"Incorrect Syntax near 'ON'" and the other was "Incorrect Syntax near 'END'".
I removed the ; characters and run again, this time I was getting no errors, and the procedure was being created successfully.
CREATE
PROCEDURE [_ACC_OVERWRITE_PROPERTY_DETAILS]
AS
BEGIN
SET NOCOUNT ON
TRUNCATE TABLE
SDE_SPATIAL.GISADMIN._ACC_TEMP
INSERT
INTO
SDE_SPATIAL.GISADMIN._ACC_TEMP
(
Parcel ,
Assessment ,
House ,
Street ,
St_Type ,
Title ,
Area ,
Area_Units ,
Suburb
)
SELECT
parc.pcl_num ,
parc.ass_num ,
STAD.HOU_NUM ,
stad.str_nme ,
stad.str_typ ,
parc.fmt_ttl ,
aps.property_area ,
LOWER(aps.AREA_INDICATOR) + '²',
stad.sbr_nme
FROM
GAVIN..AUTH.AUPRSTAD stad,
GAVIN..AUTH.AUSRMAST mast,
GAVIN..AUTH.AV_PROPERTY_SUMMARY aps,
GAVIN..AUTH.AUPRPARC parc
WHERE
PARC.PCL_NUM=STAD.PCL_NUM
AND STAD.STR_NUM=MAST.STR_NUM
AND (
PARC.PCL_FLG='R'
OR PARC.PCL_FLG='P')
AND PARC.PCL_NUM=aps.PARCEL_NUMBER
AND stad.SEQ_NUM = 0
END
This is how I would do it with a SQL Server stored procedure (sorry, but I don't think you named your particular SQL dialect):
CREATE PROCEDURE [ResetSpatialData]
AS
BEGIN
SET NOCOUNT ON;
TRUNCATE TABLE SDE_SPATIAL.GISADMIN.TO_TABLE;
INSERT INTO SDE_SPATIAL.GISADMIN.TO_TABLE
([field1], [field2], [field3], [etc], [you get the idea])
SELECT [field1], [field2], [field3], [etc], [you get the idea]
FROM GAVIN..AUTH.FROM_TABLE;
END
You could then schedule this for regular execution on some time basis by using a SQL Server Agent job.
Note: corrected as per #Nick McDermaid's suggestion.
I'm hitting my head against the wall with this one.
We have a stored procedure that is being called in an API that we are developing and the stored procedure has the following code:
if(#StatusCode = 41 and #OperationName != 'convert')
Begin
EXEC [uspCreateOrg] #RequestID = #_RequestId
End
else
Begin
EXEC [uspUpsertOrg] #RequestID = #_RequestId
End
Using the profiler, we can that the 'if' branch is the one that gets executed, but we also see that SQL Server is looking down the 'else' branch and calling into that stored procedure and throwing an exception. The uspUpsertOrg procedure calls the DBAmp BulkOps which has the following code in it:
create table #errorlog (line varchar(255))
insert into #errorlog
exec #Result = master..xp_cmdshell #Command
-- print output to msgs
declare #line varchar(255)
declare #printCount int
set #printCount = 0
DECLARE tables_cursor CURSOR FOR SELECT line FROM #errorlog
OPEN tables_cursor
FETCH NEXT FROM tables_cursor INTO #line
WHILE (##FETCH_STATUS <> -1)
BEGIN
if #line is not null
begin
print #line
exec SF_Logger #SPName,N'Message', #Line
set #errorLines = #errorLines + #line
set #printCount = #printCount +1
end
FETCH NEXT FROM tables_cursor INTO #line
END
deallocate tables_cursor
-- drop temp output table
drop table #errorlog
The exception that gets thrown is that the #errorLog table does not exist. So in summary we are getting an exception that a temp table created on the line above the insert does not exist in a stored procedure that does not even get called...Fun...
When we comment out the call to uspUpsertOrg everything works as expected. When we change the temp table to a real table, it still fails, but if we create it outside the procedure and then run the process, it works. In any of these cases, the code does not go down the 'else' branch in the sense that the 'else' branch would be the one that gets executed. It's almost as if SQL server is looking ahead down all code paths--I know that SQL Server does that kind of thing for optimization, etc, but why would it miss the fact that the table IS being created before use? I've done this kind of thing before and never had problems.
Thanks for the help!
According to this article on Execution Plan Basics, this exact scenario causes a problem for the algebrizer that doesn't execute your code, but is responsible for generating the execution plan. Look for the section When the Estimated Plan is Invalid.
I think this workaround will work for you: Instead of the CREATE statement, use
SELECT CAST('' as VARCHAR(255)) as line INTO #errorlog
I am using SQL Server 2008 R2 and I want to create a stored procedure that deletes from two tables using a paramater (id).
This is the stored procedure:
CREATE PROCEDURE [dbo].[sp_deleteDecision]
#ID int
AS
DELETE FROM [dbo].[tblDecisionInvolvement] as di
WHERE di.DecisionId = #ID
DELETE FROM [dbo].[tblDecision] as d
WHERE d.Id =#ID
GO
This is the error I get when I try to create it:
Msg 156, Level 15, State 1, Procedure sp_deleteDecision, Line 6
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Procedure sp_deleteDecision, Line 8
Incorrect syntax near the keyword 'as'.
Note that changing the DELETE FROM to
SELECT * FROM
it works.
Is it even possible to delete something using parameters?
Ty.
You aren't allowed to introduce an alias at that part of a DELETE statement - nor do you need one in this case:
USE ChessDb01
GO
CREATE PROCEDURE [dbo].[sp_deleteDecision]
#ID int
AS
DELETE FROM [dbo].[tblDecisionInvolvement]
WHERE DecisionId = #ID
DELETE FROM [dbo].[tblDecision]
WHERE Id =#ID
GO
For a more complex query, you might want to use an alias, but note that (confusingly), the DELETE will have two FROM clauses - and you can only introduce the alias in the second one:
DELETE FROM di
FROM [dbo].[tblDecisionInvolvement] di
inner join
AnotherTable tab
on
di.Column = tab.Column2
WHERE tab.Column99 = #Value
I am trying to create a BEFORE INSERT trigger that will check the incoming value of a field, and replace it with the same field in another row if that the field is null. However, when I add the CALL statement to my trigger, an error is returned "The trigger "ORGSTRUCT.CSTCNTR_IN" is defined with an unsupported triggered SQL statement". I checked the documentation and saw that cursors weren't supported in the BEFORE (part of the reason for making the stored procedure in the first place), but even when I remove the cursor declaration from the stored procedure the call still generates the same error.
Trigger:
CREATE TRIGGER orgstruct.cstcntr_IN
NO CASCADE
BEFORE INSERT ON orgstruct.tOrgs
REFERENCING NEW AS r
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
DECLARE prnt_temp BIGINT;
DECLARE cstcntr_temp CHAR(11);
SET prnt_temp = r.prnt;
SET cstcntr_temp = r.cstcntr;
CALL orgstruct.trspGetPrntCstCntr(prnt_temp,cstcntr_temp);
SET r.cstcntr = cstcntr_temp;
END
Stored procedure:
CREATE PROCEDURE orgstruct.trspGetPrntCstCntr (
IN p_prnt BIGINT,
OUT p_cstcntr CHAR(11)
)
SPECIFIC trGetPrntCstCntr
BEGIN
IF p_prnt IS NULL THEN
RETURN;
END IF;
BEGIN
DECLARE c1 CURSOR
FOR
SELECT cstcntr
FROM orgstruct.tOrgs
WHERE id = p_prnt
FOR READ ONLY;
OPEN c1;
FETCH FROM c1 INTO p_cstcntr;
CLOSE c1;
END;
END
According to the documentation, CALL is allowed in a BEFORE trigger, so I don't understand what the problem is.
A before trigger can call a stored procedure, but the stored proc can't do anything not allowed in the trigger.
In your case, the default level of data access for a SQL stored proc is MODIFIES SQL DATA, which is not allowed in the trigger. You could recreate your stored procedure, changing the data access level to READS SQL DATA; this will allow you to create the trigger.
However: There is no reason to call a stored procedure for something this simple; You can do it using a simple inline trigger:
create trigger orgstruct.cstcntr_IN
no cascade
before insert on orgstruct.tOrgs
referencing new as r
for each row
mode db2sql
set r.cstcntr = case
when r.p_prnt is not null
then (select cstcntr from tOrgs where id = r.p_prnt fetch first 1 row only)
else r.cstcntr
end;
This will be a LOT more efficient because it eliminates both the stored procedure call and the cursor processing inside the stored proc. Even if you wanted to use the stored proc, you could eliminate the cursor inside the stored proc and improve performance.
FYI: the logic that you posted contains an error, and will always set CSTCNTR to NULL. The trigger posted in this answer not do this. :-)
I cannot run the following SP
CREATE PROCEDURE SP_NYANSAT(
FORNAVN VARCHAR(30),
EFTERNAVN VARCHAR(30),
ADRESSE VARCHAR(50),
POSTNUMMER CHAR(4),
TELEFONNUMMER CHAR(8),
EMAIL VARCHAR(50))
AS
DECLARE VARIABLE ID INTEGER;
BEGIN
ID = GEN_ID(GEN_ANSAT_ID,1);
INSERT INTO MYTABLE (ID, FORNAVN, EFTERNAVN, ADRESSE, POSTNUMMER, TELEFONNUMMER, EMAIL) VALUES (:ID, :FORNAVN, :EFTERNAVN, :ADRESSE, :POSTNUMMER, :TELEFONNUMMER, :EMAIL);
END
The error I get is the following:
can't format message 13:896 -- message file C:\Windows\firebird.msg not found.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 3, column 1.
CREATE.
Have you used Set Term before and after this code?
All commands in Firebird must be terminated with a semi-colon. If you want to create a stored procedure you need to be able to distinguish between the terminating semi-colon from the semi-colons inside the stored procedure.
Something like this:
SET TERM ^ ;
CREATE PROCEDURE SP_NYANSAT(
FORNAVN VARCHAR(30),
EFTERNAVN VARCHAR(30),
ADRESSE VARCHAR(50),
POSTNUMMER CHAR(4),
TELEFONNUMMER CHAR(8),
EMAIL VARCHAR(50))
AS
DECLARE VARIABLE ID INTEGER;
BEGIN
ID = GEN_ID(GEN_ANSAT_ID,1);
INSERT INTO MYTABLE (ID, FORNAVN, EFTERNAVN, ADRESSE, POSTNUMMER, TELEFONNUMMER, EMAIL) VALUES (:ID, :FORNAVN, :EFTERNAVN, :ADRESSE, :POSTNUMMER, :TELEFONNUMMER, :EMAIL);
END
^
SET TERM ; ^
Please notice how the declaration of the stored procedure is terminated with ^, thus ending the statement. After the declaration you also restore the terminating semi-colon.
On a side note, I would recommend to copy firebird.msg to the location the error you get tells you so you can see what is really happening.
EDIT:
If you wish you can check this link. There you can find a lot of information regarding Firebird + IBExpress, including SET TERM (page 81).
EDIT 2:
Just tried at home with IBExperts + Firebird and I had no problem creating the stored procedure. My guess is you are trying to do one of the following things:
You have opened an SQL editor and are trying to compile the code directly. That will not work because IBExperts thinks you are trying to run DSQL sentences. Stored procedures are created with PSQL sentences.
You are trying to use the "New procedure" utility (check buttons in the upper right side of the main menu) and pasted the whole code into the editor. That will not work because in that editor you only have to put the body code. Stored procedure name is set in a field on the upper right side of the window you opened. Parameters and variables are introduced by using the "Insert Parameter/Variable" button on the left side above the code editor. The SET TERM sentences are created automatically by IBExperts. You can check the resulting code in the DDL tab.
HTH