How does one handle scope identity statements in T-SQL T? - tsqlt

There doesn't seem to be any way to Fake a table then have the SCOPE_IDENTITY function run on the faked table.
Take this statement (inside a stored procedure)
INSERT INTO [dbo].[SomeTable](Id1, Id2)
VALUES (#Id1, #Id2)
SET #SomeIdentity = SCOPE_IDENTITY()
INSERT INTO [dbo].[Table2](Id3)
VALUES(#SomeIdentity)
So if I try and test this block of code
I'll fake the table [SomeTable] like this
EXEC [tSQLt].[FakeTable] #TableName = N'[SomeTable]',
#SchemaName = N'dbo',
#Identity = 0,
#ComputedColumns = 1,
#Defaults = 1
EXEC [tSQLt].[FakeTable] #TableName = N'[Table2]',
#SchemaName = N'dbo',
#Identity = 0,
#ComputedColumns = 1,
#Defaults = 1
However when it comes to test the block of code with the scope identity column, the Id3 is always null.
I've attempted identity = 0 or 1 but T-SQL doesn't seem to handle this scenario..
Has anyone solved this issue?

You need to set the #identity parameter to 1 to preserve the identity column when using tSQLt.FakeTable:
EXEC [tSQLt].[FakeTable]
#TableName = N'dbo.[SomeTable]',
#Identity = 1,
#ComputedColumns = 1,
#Defaults = 1
(Note: the #SchemaName parameter is deprecated.)

Related

Calling procedure: Parameter index of 2 is out of range (1, 1)

I call stored procedure used:
#Entity
#Table(name = "web_id_idx")
#NamedStoredProcedureQueries({
#NamedStoredProcedureQuery(name= "check_web_id",
procedureName= "check_web_id",
parameters = {
#StoredProcedureParameter(mode = ParameterMode.IN, name = "web_id", type = String.class),
#StoredProcedureParameter(mode = ParameterMode.OUT, name = "query_web_id", type = Integer.class),
})
})
and use entity manager to call it: Object result = em.createNamedStoredProcedureQuery("check_web_id");
getQuery.setParameter("web_id", webid);
It always says: Parameter index of 2 is out of range (1, 1)
Here's the procedure that I tested on mysql, it returns either 0 or 1:
DROP PROCEDURE IF EXISTS check_web_id;
DELIMITER ;;
CREATE DEFINER=root#localhost PROCEDURE check_web_id(web_id VARCHAR(25))
begin
SET #id_var = web_id;
SET #query_check_id = CONCAT("SELECT COUNT(*) AS total FROM web_id_idx WHERE web_id = '", #id_var,"'");
PREPARE stmt FROM #query_check_id;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
end;;
DELIMITER ;
Anyone knows what is wrong?
Thanks,
Mo.

Select Top 1 From a Table For Each row in another Table

I am just starting to work with openedge and I need to join information from two tables but I just need the first row from the second one.
Basically I need to do a typical SQL Cross Apply but in progress. I look in the documentation and the Statement FETCH FIRST 10 ROWS ONLY only in OpenEdge 11.
My query is:
SELECT * FROM la_of PUB.la_ofart ON la_of.empr_cod = la_ofart.empr_cod
AND la_of.Cod_Ordf = la_ofart.Cod_Ordf
AND la_of.Num_ordex = la_ofart.Num_ordex AND la_of.Num_partida = la_ofart.Num_partida
CROSS APPLY (
SELECT TOP 1 ofart.Cod_Ordf AS Cod_Ordf_ofart ,
ofart.Num_ordex AS Num_ordex_ofart
FROM la_ofart AS ofart
WHERE ofart.empr_cod = la_ofart.empr_cod
AND ofart.Num_partida = la_ofart.Num_partida
AND la_ofart.doc1_num = ofart.doc1_num
AND la_ofart.doc2_linha = ofart.doc2_linha
ORDER BY ofart.Cod_Ordf DESC) ofart
I am using SSMS to extract data from OE10 using an ODBC connector and querying to OE using OpenQuery.
Thanks for all help.
If I correctly understood your question, maybe you can use something like this. Maybe this isn't the best solution for your problem, but may suit your needs.
DEF BUFFER ofart FOR la_ofart.
DEF TEMP-TABLE tt-ofart NO-UNDO LIKE ofart
FIELD seq AS INT
INDEX ch-seq
seq.
DEF VAR i-count AS INT NO-UNDO.
EMPTY TEMP-TABLE tt-ofart.
blk:
FOR EACH la_ofart NO-LOCK,
EACH la_of NO-LOCK
WHERE la_of.empr_cod = la_ofart.empr_cod
AND la_of.Cod_Ordf = la_ofart.Cod_Ordf
AND la_of.Num_ordex = la_ofart.Num_ordex
AND la_of.Num_partida = la_ofart.Num_partida,
EACH ofart NO-LOCK
WHERE ofart.empr_cod = la_ofart.empr_cod
AND ofart.Num_partida = la_ofart.Num_partida
AND ofart.doc1_num = la_ofart.doc1_num
AND ofart.doc2_linha = la_ofart.doc2_linha
BREAK BY ofart.Cod_Ordf DESCENDING:
ASSIGN i-count = i-count + 1.
CREATE tt-ofart.
BUFFER-COPY ofart TO tt-ofart
ASSIGN ofart.seq = i-count.
IF i-count >= 10 THEN
LEAVE blk.
END.
FOR EACH tt-ofart USE-INDEX seq:
DISP tt-ofart WITH SCROLLABLE 1 COL 1 DOWN NO-ERROR.
END.

Converting an PLSQL select statement into a update

Guys I have such a problem.
I know how to write a good select statement but I have no idea how to turn it into a corresponding update.
Im still learning plsql
Here is my select
select * --count(*)
from POLISY_OT ot
join polisy p on p.poli_id = ot.ot_poli_id
join sou.rai_skl rs on rs.ot_id = ot.ot_id
where ot_under_promil = 0
and ot_skladka_rok <> ot_skladka_netto_rok
and ot_rodzaj_um = 'OP'
and ot_rodzaj = 'D'
and ot_produkt_id = 17
and p.poli_status in ('AK', 'CZ')
and rs.skl_roczna = ot.ot_skladka_rok;
now I would like to wrap it up with an update and create something like this
update (
select * --count(*)
from POLISY_OT ot
join polisy p on p.poli_id = ot.ot_poli_id
join sou.rai_skl rs on rs.ot_id = ot.ot_id
where ot_under_promil = 0
and ot_skladka_rok <> ot_skladka_netto_rok
and ot_rodzaj_um = 'OP'
and ot_rodzaj = 'D'
and ot_produkt_id = 17
and p.poli_status in ('AK', 'CZ')
and rs.skl_roczna = ot.ot_skladka_rok)
set ot_skladka_rok = ot_skladka_netto_rok;
First, I really hope you aren't a student asking for homework help. That really bugs me.
On the assumption it's not, it's a little hard to tell exactly which columns belong to which tables, given that you didn't include the table aliases throughout.
I read this as that you wanted to update a column based on the value in another table, restricting the table updates to records that match a third table).
So I think you want something like this:
UPDATE polisy_ot ot
SET ot_skladka_rok =
(SELECT ot_skladka_netto_rok
FROM sou.rai_skl rs
WHERE rs.ot_id = ot.ot_id
AND rs.skl_roczna = ot.ot_skladka_rok)
WHERE ot_under_promil = 0
AND ot_skladka_rok <> ot_skladka_netto_rok
AND ot_rodzaj_um = 'OP'
AND ot_rodzaj = 'D'
AND ot_produkt_id = 17
AND EXISTS (SELECT NULL
FROM polisy p
WHERE p.poli_id = ot.ot_poli_id
AND p.poli_status IN ('AK', 'CZ'))
Good luck,
Stew

How to segregate a stored procedure into a table row by row?

There is a stored procedure which contains many insert, update, delete and truncate statements. I want to group all the statements one by one into a table.
For example:
create proc Get_Tables as
begin
UPDATE BB_FMCTransactionsTwo SET wsTradeDate = wsSettleDate WHERE wsEntryCode = 'NRT'
UPDATE BB_FMCTransactionsTwo SET wsOK = 1, wsSpecialLogic = SpecialLogic FROM BB_EntryCode INNER JOIN BB_FmcTransactionsTwo ON EntryCode = wsEntryCode
UPDATE BB_FMCTransactionsTwo SET wsFMCFtNt = '' FROM BB_EntryCode INNER JOIN BB_FmcTransactionsTwo ON EntryCode = wsEntryCode WHERE wsBS <> 'B' AND ( FMCFtNtB IS NULL OR FMCFtNtB = 'PR' )
UPDATE BB_B204_Tran SET AMOUNT = dbo.BCA_AMT(Desc1, Desc2, Desc3, Udesc1, Udesc2, Udesc3) WHERE TRANIND = 14 AND PORTTYPE IN (2,3)
DELETE BB_B204_Tran WHERE TRANIND = 14 AND PORTTYPE IN(2,3) AND (Quantity IS NULL OR ISNUMERIC(Quantity) = 0 OR Quantity = -1 OR ISNUMERIC(AMOUNT) = 0
end
Required:
I want to insert the update statements, delete statements into a table one by one...
Create a stored procedure which divides the stored procedure based on type of statements (Insert, Update, Truncate) and inserts that into a table row by row. So that the table looks like below.
Table:
Sl.No Statement
1 UPDATE BB_FMCTransactionsTwo SET wsTradeDate = wsSettleDate WHERE wsEntryCode = 'NRT'
2 DELETE BB_B204_Tran WHERE TRANIND = 14 AND PORTTYPE IN(2,3)
I have implemented for your first update statement and assume that your AuditTable will have one column SQLData and so you need to store these SQL Statements in your store procedure and if you have used any variable then you need to store into SQL Data variable and insert into Audit Table. it may help you.
declare ##SQL1 varchar(max)
UPDATE BB_FMCTransactionsTwo SET wsTradeDate = wsSettleDate WHERE wsEntryCode = 'NRT'
set ##SQL1 = 'UPDATE BB_FMCTransactionsTwo SET wsTradeDate = wsSettleDate WHERE wsEntryCode = ''NRT'''
insert into AuditTable (SQLData) values(##SQL1)
For Variable:
DECLARE ##wsEntryCode VARCHAR(100)
SET ##wsEntryCode='NRT'
PRINT 'UPDATE BB_FMCTransactionsTwo SET wsTradeDate = wsSettleDate WHERE wsEntryCode = ''' + ##wsEntryCode + ''''
insert into AuditTable (SQLData) values(##SQL1)

single-row subquery returns more than one row .what should i do in such case

UPDATE STG_ABS_DSD_RECEIPTS_PI_WRK1 A
SET
(
RECEIPT_HEADER_KEY,
ORIG_ACNTNG_EFF_DATE,
GL_DEPT_ID,
RECEIPT_DATE,
RECEIPT_TIME,
TOTAL_INVOICE_COST_HDR,
SUM_EXTENDED_COST_AMT
) =
(SELECT
HDR_RECEIPT_HEADER_KEY,
HDR_ORIG_ACNTNG_EFF_DATE,
HDR_GL_DEPT_ID,
HDR_RECEIPT_DATE,
HDR_RECEIPT_TIME,
HDR_SUM_TOTAL_INVOICE_COST,
PIEDW_EXTENDED_COST_AMT
FROM
STG_ABS_DSD_RECEIPTS_PI_WRK4 B
WHERE
A.SUPPLIER_KEY = B.PIEDW_SUPPLIER_KEY
AND
A.STORE_KEY = B.PIEDW_STORE_KEY
AND
RTRIM(LTRIM(A.SUPPLIER_INVOICE_NBR,0)) = RTRIM(LTRIM(B.PIEDW_SUPPLIER_INVOICE_NBR,0))
AND
TO_DATE(A.PIEDW_INV_TRAN_DATE,'YYYYMMDD') = B.PIEDW_INVOICE_DATE
AND
B.HDR_FOUND_FLAG IN ('N', 'MY'))
WHERE EXISTS
(SELECT 1 FROM STG_ABS_DSD_RECEIPTS_PI_WRK4 B
WHERE
A.SUPPLIER_KEY = B.PIEDW_SUPPLIER_KEY
AND
A.STORE_KEY = B.PIEDW_STORE_KEY
AND
RTRIM(LTRIM(A.SUPPLIER_INVOICE_NBR,0)) = RTRIM(LTRIM(B.PIEDW_SUPPLIER_INVOICE_NBR,0))
AND
TO_DATE(A.PIEDW_INV_TRAN_DATE,'YYYYMMDD') = B.PIEDW_INVOICE_DATE
AND
B.HDR_FOUND_FLAG IN ('N', 'MY'));
You should ensure that the subquery only returns the row that you want by providing the correct joins from the table you are updating, or if you just want one row out of many that might be returned then use "WHERE ROWNUM = 1" (or a LIMIT clause in other RDBMSs or Oracle 12c)

Resources