I am new to Sql Server and having difficulty to convert the oracle triggers to Sql server.
Can some one help me with that.Here is one of the example:
create or replace
TRIGGER "NEW".TRG_dummy
BEFORE INSERT ON TBL_dummy
FOR EACH ROW
BEGIN
SELECT SEQ_dummy.NEXTVAL
INTO :NEW.dummy_ID
FROM DUAL;
END;
Any help in this will be appreciated.It would be great if I get a method to convert as I have a lot of other objects to be migrated.
Replacing of the trigger doesn't make sense here since you don't even need a trigger for the same functionality in SQL Server. You only need to declare the column as an IDENTITY, e.g.
CREATE TABLE TBL_AFR (
AFR_ID INT IDENTITY NOT NULL, --<< this automatically does the same thing
.. the other columns
);
Related
i have some querys in my new SQL 2019 Database that connect to a table in a different database (MSSQL Server 2016) via linked servers.
But Everytime there is a date field in that query i get the error "The function or aggregat .ToString could not be found or the name is ambiguous".
I found that there is a hierarchy-ID-Function named "toString".
But i cant see why SQL should be using it when i dont call it to.
When i delete all Date fields from that query it executes without a problem.
SELECT * FROM [LinkedServerName].Databasename.integris.Tablename
EDIT:
I just noticed, that SQL somehow edits my text.
is what i wrote into the vieweditor.
is what the error shows me.
EDIT2: i set up the same linked server on an old SQL 2005 Server. Everything works fine. I guess because the 2005 server has no ToString() function... but im not sure.
I can construct a situation that would recreate the error, but I can't say for sure whether this is what is happening in your case. It's hard to demonstrate in a comment though, so I'll do as an answer.
Suppose we have a table t, with a computed column f, which references function dbo.f, which in turn references function dbo.g:
create function dbo.g() returns int as begin return 1 end;
go
create function dbo.f() returns int as begin return dbo.g(); end;
go
create table t(i int, f as dbo.f());
go
insert t(i) values (1);
select * from t;
go
So far so good. But now we drop function dbo.g and try to select from t:
drop function dbo.g;
select * from t;
This will result in the error: Cannot find either column "dbo" or the user-defined function or aggregate "dbo.g", or the name is ambiguous.
Look at the definition of the source table and check for computed columns, especially computed columns using nested constructs.
I need to get a userstamp into a table and have not managed to figure out how the GENERATED FOR EACH ROW ON UPDATE AS statement works with the SESSION_USER variable in DB2 10.5 (LUW).
Managed to get an implementation working using a function which has a fake variable for forcing the evaluation in update statements:
CREATE OR REPLACE FUNCTION XXX.CURRENT_USER( tmp varchar(128))
SPECIFIC xxx.XXX_CURRENT_USER
RETURNS VARCHAR(128)
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
BEGIN
RETURN session_user ;
END
GO
CREATE TABLE xxx (
i INTEGER,
t VARCHAR(128) GENERATED ALWAYS AS (XXX.CURRENT_USER(i))
)
However, would be nice have less "hacky" implementation for a basic thing like this.
For the time stamps there is that "FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP" statement but no equivalent for other register variables it seems.
Help is very much appreciated
Does this work?
CREATE TABLE xxx (
i INTEGER,
t VARCHAR(128) WITH DEFAULT session_user
);
I don't have DB2 on hand to check, but this is very similar to the syntax used in other databases (although the more typical syntax does not use WITH).
I am relatively newbie with SQL Server, and I have some experience and practices from Oracle & PostgreSQL which i want to use in the SQL Server.
I need to create function which takes fields values for new row in the table and which also returning autogenerated ID value of the new record.
First of all I am faced with the fact that the functions in SQL Server can not change data in the tables. The second my discovery was that is the procedures in SQL Server can return values through return #result construction.
I investigate output mechanism of the DML queries, but they returns not scalar but table results.
Be patient and let me more clear. There is PostgreSQL function which doing what I want:
Table creation script:
create table foobar
(
foo bigint not null default nextval('s_foobar'::regclass),
bar character varying(16),
constraint pk_foobar primary key (foo)
);
and function script:
create or replace function f_foobar_insert(p_bar character varying)
returns integer as
$body$declare
result integer;
begin
insert into foobar(bar) values (p_bar) returning foo into result;
return result;
end;$body$ language plpgsql;
Is there any possibility to make something like this in SQL Server in the same way?
In SQL Server, the table creation would be:
create table foobar
(
foo bigint not null identity primary key,
bar varchar(16)
);
The following is one way in SQL Server to get the functionality:
insert into foobar(bar) select 'value';
select ##identity;
This is not really the preferred way. You should really use the output clause:
declare #t table (foo bigint);
insert into foobar(bar)
output inserted.foo into #t
select 'value';
select foo from #t;
You can wrap this in a stored procedure if you like, but it doesn't seem necessary, and stored procedures have different semantics from functions.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Get ID of last inserted record in oracle db
I am brand new to oracle, having used SQL Server in the past. I have a stored procedure, and I am trying to do two INSERTs one after the other. The second INSERT requires the ID of the first INSERT. Could someone explain how to do it?
I'm seeing stuff about SEQUENCEs and nextvalue/curval ?
I guess in SQL Server I'd just declare a variable, and use SCOPE_IDENTITY, so I'd be looking to do that.
There are two possibilities. If you are using sequences for number generation, you can get the value from the sequence, like this:
select SequenceName.currval into AVariable from dual
or this
AVariable := SequenceName.currval;
But it's probably better to use the returning clause like this:
declare
AVariable int;
begin
insert into yourtable(columns)
values (values)
returning id into AVariable;
insert into anotherTable(columns)
values(AVariable, othervalues);
end;
This way, it will work regardless of implementation details. You don't have to know the name of the sequence, and it will also work with the later introduced identity columns.
The only thing it won't work for is views with an instead of trigger, but that's a special case altogether.
There are a few approaches. One option is to use the RETURNING clause, i.e.
DECLARE
l_generated_id INTEGER;
BEGIN
INSERT INTO table_name( <<column list>> )
VALUES( <<values list>>
RETURNING <<name of primary key column>>
INTO l_generated_id;
INSERT INTO other_table( <<column list>> )
VALUES( l_generated_id, <<other values>> );
END;
If you know that the primary key is populated via a sequence (with or without a trigger) and you know the name of that sequence, you can use the sequence_name.currval in your second INSERT statement (the first INSERT statement would, either directly or via a trigger reference the sequence_name.nextval to generate the new primary key).
what do Stored Procedures and Triggers in data base mean ?
how can i create Stored Procedures ?
how can i crest Triggers ?
if you have simple examples for each of these .please help :)
what i know is only about trigger which is activated if an action of(insert or delete or update ) violates the constrains specified but i don't know how to create ,so again if any have example please
Think of a Stored Procedure as a method in your code. It runs a specific set of instructions.
Stored Procedures are created to, for example, manage complex sets of data that would normally be a pain to handle along in your code.
You can create a Stored Procedure with the following instructions:
Oracle
CREATE OR REPLACE PROCEDURE P_PROCEDURE_NAME (
pParameter1 NUMBER
, pParameter2 VARCHAR2(100 Bytes)
) AS
BEGIN
-- Procedure code here...
END;
SQL Server
CREATE PROCEDURE cspProcedureName
#parameter1 int
, #parameter2 nvarchar(100)
AS
-- Procedure code here...
Oracle
As for the Triggers, they are sets of code called upon an action occuring to the related table. For instance, in Oracle, there are no INDENTITY columns such as SQL Server offers. Instead, Sequences are used along with Triggers to simulate the same. Hence, you will need to create an Oracle SEQUENCE, then the TRIGGER to update the ID field of your table.
CREATE SEQUENCE SEQ_CUSTOMERS
MINVALUE 1
MAXVALUE 65535
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER TRG_CUSTOMERS_INSERT
BEFORE INSERT
ON TBL_CUSTOMERS
FOR EACH ROW
BEGIN
:NEW.CUST_ID := SEQ_CUSTOMERS.NEXTVAL;
END;
SQL Server
A trigger example in SQL Server would be updating automatically the update datetime of a record. Consider the following:
CREATE TABLE Customers (
CustId int NOT NULL IDENTITY(1, 1) PRIMARY KEY
, CustName nvarchar(100) NOT NULL
, CreatedOn datetime DEFAULT GETDATE()
, LastUpdate datetime NOT NULL
)
GO
CREATE TRIGGER trgCustomersUpdt
AFTER UPDATE
ON Customers
AS
update Customers
set LastUpdate = GETDATE()
where CustId = inserted.Custid
GO
DISCLAIMER
This code has not been tested and may require minor changes for it to work properly against its respective RDBMS.
To sum it up, Triggers are mainly used to as illustrated here, despite there are many other possible use, such as building up an history of table changes that occured throught time, keeping all records of transactions into an history table or the like. The Stored Procedures are mainly used to perform complex database tasks where this would get too complex to do in code.