SQL Insert ,Update using Merge Store Procedure - sql

I need to do a bulk Insert and/or Update for a target table in a SQL database. I have already tried the Merge stored procedure. In the target table I have a composite key.
I need to generate a pattern for first field (of the key) while inserting. I have tried a user-defined function that returns a pattern key which is unique for each of the rows.
My question is, how do I incorporate this function into a query that would auto-generate the key and insert the respective fields for each record?
TargetTable is my destination table in database and it has two columns - Key1(Bigint),Key2(int). Once again, this is a composite key.
Note: I only want to generate the key for the first field/column.
Here's my stored procedure:
Merge Targettable as T
Using SourceTable as s
ON s.Key1 = T.Key1 and
s.Key2=T.Key2
WHEN MATCHED THEN UPDATE
SET T.UserName = S.UserName
WHEN NOT MATCHED THEN
INSERT (Key1, Key2, UserName)
VALUES (dbo.UserDefiendFunction(#parama1),Key2,userName)
UserDefinedFunction returns the pattern i want.
Here's my User Defined Function:
Create function [dbo].[UserDefinedFunction] (
#Param1 int)
returns BIGINT
AS Begin
DECLARE #ResultVar BIGINT
SET #ResultVar=CONVERT(BIGINT, (SELECT Cast(#Param1 AS VARCHAR(10))
+ '' + Format((SELECT Count(userName)+1 FROM [dbo].[TableName]), '0')) )
RETURN #ResultVar
END
Please help me out with this. Thanks in advance.

IF EXISTS (SELECT * FROM Table1 join table 2 on Column1='SomeValue')
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
insert into ods..Table1 (Columname)
select [DatabaseName].[dbo].[UserDefinedFunction] (#paramater) as ColumnName

Related

Create a string from inserted.id via trigger

I'm creating a trigger on insert that will create a new record in another table using the id from the newly inserted row.
I need to format the id before I insert it into the other table. This is my code so far.... having problems creating the formated #PseudoID.
CREATE TRIGGER OnInsertCreateUnallocatedPseudo
ON tblTeams
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
DECLARE #PseudoID NVARCHAR(50), #tmID NVARCHAR(10)
SELECT #tmID = CONVERT(nvarchar(10),inserted.tmID) FROM inserted
--NEED SOME CODE TO CREATE A PADDED OUT PseudoID e.g.
-- if #tmID = '7' then #PseudoID = 'PSU0007'
-- if #tnID = '25' then #PseudoID = 'PSU0025'
INSERT INTO [dbo].[tblUsersPseudo].....
END
You can't assign it to a variable within the trigger since there could be multiple rows, but you could do something like this to insert into the other table.
LEFT('PSU0000',LEN('PSU0000')-LEN(CONVERT(nvarchar(10),inserted.tmID))) + CONVERT(nvarchar(10),inserted.tmID)
You can't assume that Inserted has only a single row, you have to treat it as a table with 0-N rows and carry out set-based operations on it (rather than procedural).
FORMAT will format your existing id into the new format you require.
INSERT INTO [dbo].[tblUsersPseudo] (id, col1, col2, ...)
SELECT FORMAT(id,'PSU0000')
, col1, col2
FROM Inserted;

Microsoft SQL Server - default value provided by stored procedure

Is it possible to have a non-null column where the value is generated at insert by calling a stored procedure the parameters of which are values passed to insert into the row?
For example, I have table User:
| username | name | surname | id |
Insert looks like this:
INSERT INTO USER (username, name, surname)
VALUES ('myusername', 'myname', 'mysurname');
The id column is populated with an (integer) value retrieved by calling stored procedure mystoredproc with parameters myusername, myname, mysurname.
A further question is, would this stored procedure be called on each row, or can it be called in a grouped fashion. For example, I'd like my stored procedure to take the name and append a random integer to it so that that if I insert 100 users with the name 'David', they will get the same id and the stored procedure will be called only once. A bit of a bad example on the second point.
Good day,
Is it possible to have a non-null column where the value is generated at insert by calling a stored procedure
Option 1: please check if this work for you
Specify Default Value for the Column and use "NOT NULL"
create trigger on the table AFTER INSERT
Inside the trigger, you can use the virtual table "inserted" in order to get the inserted values.
Using these values (using the inserted table) you can update the column using the logic you need for all the rows at once
** there is no need to use external SP probably, but you can execute SP from trigger if needed
** All executed by a trigger is in the same transaction as the original query.
would this stored procedure be called on each row
NO! The trigger will be executed once for all rows you insert in the same statement. The inserted table includes all the rows which were inserted. In your update section (step 4) you can update all the rows which were inserted in once and no need to execute something for each row
** If you do use external SP which is executed from the trigger then you can pass it all the inserted table as one using Table-Valued Parameter
------------------- update ---------------
Here is a full example of using this logic:
drop table if exists T;
CREATE TABLE T (id int identity(2,2), c int NOT NULL default 1)
GO
CREATE TRIGGER tr ON T AFTER INSERT
AS BEGIN
SET NOCOUNT ON;
UPDATE T SET T.c = T2.C + 1
FROM inserted T2
INNER JOIN T T1 ON T1.id = T2.id
END
INSERT T(c) values (1) -- I insert the value 1 but the trigger will change it to 1+1=2
select * from T
GO
-- test multiple rows:
INSERT T(c) values (10),(20),(30),(40)
select * from T
GO
DECLARE #rc INT = 0,
#UserID INT = ABS(CHECKSUM(NEWID())) % 1000000 + 1;
WHILE #rc = 0
BEGIN
IF NOT EXISTS (SELECT 1 FROM dbo.Users WHERE UserId= #UserId)
BEGIN
INSERT dbo.Users(UserId) WHERE Username = #UserName SELECT #UserId;
SET #rc = 1;
END
ELSE
BEGIN
SELECT #UserId = ABS(CHECKSUM(NEWID())) % 1000000 + 1,
#rc = 0;
END
END

Update and Insert Trigger

I have four tables which constantly truncate then inserts and updates data every five minutes from another system into SQL Server. So I need to create an update and insert trigger to update another in SQL Server using if exists or merge.
Select
[Barcode]
,[TradingPartnerOrderNo]
,[ProductNo]
,[BarcodeCreatedDateTime]
,[BarcodeScheduledDateTime]
,[CancelledIndicator]
,[CancelledDateTime]
,[SiteIndicator]
,[InterfaceTimeStamp]
,[OperationDate]
,[Operation]
from [dbo].[BookingView]
For me it is not very clear your demande but here is a trigger example :
CREATE TRIGGER [dbo].[up_update_mytable]
ON [dbo].[MyTable] FOR UPDATE
AS
DECLARE #ID VARCHAR(50)
SELECT #ID = ID FROM INSERTED --Use INSERTED keyword for insert and update triggers to get informations of the inserted or updated record.
UPDATE MyOtherTable SET
Column1 = 'test value',
Column2 = 'test value 2'
WHERE OtherTableID = #ID
If you are using SQL Server 2008+, then you can use MERGE to do that. Something like this:
MERGE INTO table2 AS TGT
USING
(
Select
[Barcode]
,[TradingPartnerOrderNo]
,[ProductNo]
,[BarcodeCreatedDateTime]
,[BarcodeScheduledDateTime]
,[CancelledIndicator]
,[CancelledDateTime]
,[SiteIndicator]
,[InterfaceTimeStamp]
,[OperationDate]
,[Operation]
FROM [dbo].[BookingView]
) AS SRC ON SRC.Barcode = TGT.BarCode AND
WHEN NOT MATCHED THEN
INSERT (Barcode, TradingPartnerOrderNo, ...)
VALUES (SRC.Barcode, SRC.TradingPartnerOrderNo, ...);

Get SCOPE_IDENTITY value when inserting bulk records for SQL TableType

I have following table structure, for convenience purpose I am only marking individual columns
Table_A (Id, Name, Desc)
Table_1 (Id this is identity column, Name....)
Table_2 (Id this is identity column, Table_A_Id, Table_1_Id)
The relationship between Table_1 and Table_2 is 1...*
Now I have created a table type for Table_A called TType_Table_A (which only contains Id as column and from my C# app I send multiple records). I have achieved this bulk insert functionality as desired.
What I need is when I insert records into Table_2 from TType_Table_A say with below statements, I would like to capture the Id of Table_2 for each record inserted
declare #count int = (select count(*) from #TType_Table_A); --a variable declared for TType_Table_A
if(#count > 0)
begin
insert into Table_2(Table_A_Id,Table_1_Id)
SELECT #SomeValue, #SomeValueAsParameter FROM #TType_Table_A;
end;
Now say if 2 records are inserted, I would like to capture the Id for each of these 2 records.
Any input/help is appreciated
This is what I know how it can be achieved, but I want to reduce DB calls from my app or user cursor in stored procedure
Insert record in Table_1 and return back the Id Loop.....through records and insert record in Table_2 and return back the Id
OR
Use cursor in stored procedure when inserting/selecting from TableType
I assume this is Sql Server? Then you can make use of the OUTPUT clause, like so:
declare #NewId table (MyNewId INT)
insert into Table_2(Table_A_Id,Table_1_Id)
output INSERTED.MyNewId INTO #TempTable(MyNewID)
SELECT SomeValue, SomeValueAsParameter FROM #TType_Table_A;
SELECT * FROM #NewId

Insert into Table select result set from stored procedure but column count is not same

I need something like that which is of course not working.
insert into Table1
(
Id,
Value
)
select Id, value from
(
exec MySPReturning10Columns
)
I wanted to populate Table1 from result set returned by MySPReturning10Columns. Here the SP is returning 10 columns and the table has just 2 columns.
The following way works as long as table and result set from SP have same number of columns but in my case they are not same.
INSERT INTO TableWith2Columns
EXEC usp_MySPReturning2Columns;
Also, I want to avoid adding "." as linked server just to make openquery and openrowset work anyhow.
Is there a way not to have define table strucutre in temp table (all columns with datatypes and lenght)? Something like CTE.
You could use a temporary table as a go-between:
insert into #TempTable exec MySP
insert into Table1 (id, value) select id, value from #TempTable
You could solve the problem in two steps by doing the insert from the stored procedure into a temporary table, then do the insert selecting just the columns you want from the temporary table.
Information on temporary tables: http://www.sqlteam.com/article/temporary-tables
-- Well, declare a temp table or a table var, depending on the number of rows expected
-- from the SP. This table will be basically the result set of your SP.
DECLARE #spResult AS TABLE
(
ID INT,
VALUE FLOAT,
....
);
-- Get the result set of the SP into the temp table.
INSERT #spResult EXEC STORED_PROC;
-- Now you can query the SP's result set for ID and Value;
INSERT Table1 (ID, VALUE)
SELECT ID, VALUE FROM #spResult;
You dont need to create a temporary table, you can do it with single query by creating temporary view like this
with tempView as EXEC MySPReturning10Columns insert into Table1 select id, value from tempView
The temporary view disappears as soon as the statement finishes execution