I am a student doing a project in SQL Server 2005. I am trying to write a procedure to generate and insert a primary key for an ID. I thought of a before insert trigger but I am getting errors. Can someone help me.. This is what I came up with so far:
CREATE TRIGGER Create_ResourceID
ON Resource
before INSERT
AS
DECLARE #resourceid INT
SELECT #resourceid = (max(ResourceID) + 1) FROM Resource
GO
INSERT INTO Resource(ResourceID) VALUES (#resourceid);
Must you use a trigger to do this?
Simplest thing is to get SQL Server to set the primary key ID for you, something like this:
CREATE TABLE Resource (
ResourceID int identity,
.
.
.
)
Related
I am fairly new to SQL Server and I am struggling with creating a trigger that does what I need.
We have a table that before a record is inserted we want to manipulate the input value.
Example table:
MyTable
(
[RUID] INT IDENTITY(1,1),
[CustomerName] nvarchar(200),
[CustomerStatus] INT,
[CustomerType] char(1),
[OtherFields] nvarchar(100)
)
An insert may come in like:
INSERT INTO MyTable ([CustomerName], [CustomerStatus], [CustomerType], [OtherFields])
VALUES ('LName~FName', 2, 'A', 'Other Info')
We don't have control of the source system doing the insert (a vendor product that is on its way out in a couple years so management doesn't want to spend the sums of money to have them alter it) but we need something like this to happen.
CustomerStatus that is inserted to be 2x inserted value
CustomerType - Regardless of the value sent we want to be overridden with a value of 'B'
All other columns are left as is.
So with an insert sent with the values in the example above, we would actually want this to end up in the table:
'LName~FName', 4, 'B', 'Other Info'
Any help you could provide would be greatly appreciated.
Specs:
SQL Server 2008 R2 Standard Edition (Database is in SQL Server 2000 compatibility mode though)
You basically need an Instead of trigger. The code would look something like...
CREATE TRIGGER tr_Insert_MyTable
ON MyTable
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO MyTable ([CustomerName], [CustomerStatus], [CustomerType], [OtherFields])
SELECT [CustomerName]
, [CustomerStatus] * 2 AS [CustomerStatus]
,'B' [CustomerType]
, [OtherFields]
FROM inserted
END
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
);
I am creating a stored procedure to create a new customer so for instance,
CREATE PROCEDURE Customer_Create
#customer_arg
#type_arg
AS
BEGIN
INSERT INTO Customer (Customer_id, Type_id)
VALUES (#Customer_arg,#type_arg)
End;
If I have several foreign keys in my statement and they are all ID's is there a way for me to pull the NEXT ID number automatically without having to know what it would be off the top of my head when I run the execute statement? I would like to just have it pull the fact that the ID will be 2 because the previous record was 1
EXECUTE Customer_Create 16,2
Is it something wnith output? If so how does this work code wise
I suspect that what you want to do is return the new id after the record is inserted. For that:
CREATE PROCEDURE Customer_Create (
#customer_arg,
#type_arg,
#NewCustomerId int output
) AS
BEGIN
INSERT INTO Customer(Customer_id, Type_id)
VALUES (#Customer_arg, #type_arg);
#NewCustomerId = scope_identity();
End;
There are several other choices for getting the identity, which are explained here.
To get to the last inserted IDENTITY value you should use the OUTPUT clause like this:
DECLARE #IdentValues TABLE(v INT);
INSERT INTO dbo.IdentityTest
OUTPUT INSERTED.id INTO #IdentValues(v)
DEFAULT VALUES;
SELECT v AS IdentityValues FROM #IdentValues;
There are several other mechanisms like ##IDENTITY but they all have significant problems. See my Identity Crisis article for details.
In your case you can also experiment with #IDENTITY like this
DECLARE #NextID int
--insert statement goes here
SET #NextID = ##Identity`
Here are couple good resources for getting familiar with this
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/
http://blog.sqlauthority.com/2013/03/26/sql-server-identity-fields-review-sql-queries-2012-joes-2-pros-volume-2-the-sql-query-techniques-tutorial-for-sql-server-2012/
In SQL Server 2005, how can I use database_b, do something, then use the old db database_a in TSQL? The following is my code but there is some problem with it. Who can help me to identity the problem? Great thanks.
DECLARE #old_database_name VARCHAR(200)
SET #old_database_name = db_name()
use mydatabase
create table t1(id int identity(1,1))
use #old_database_name
You'll need to use dynamic sql to do this.
e.g.
-- Do stuff in current DB here
EXECUTE ('USE mydatabase; create table t1(id int identity(1,1));')
-- Do more stuff in current DB here. This context will not have changed since before the EXECUTE statement
Why are you even using USE statements for this?
create table mydatabase.dbo.t1(id int identity(1,1))
This is similar to AdaTheDev's "EXECUTE" example, but uses sp_executesql :
-- Do stuff in current DB here
EXECUTE mydatabase..sp_executesql N'create table t1(id int identity(1,1));'
-- Do more stuff in current DB here. This context will not have changed since before the EXECUTE statement
I need to write a trigger that rounds a value whenever it is inserted/updated into a table
Here is a getting started tutorial on how to use Triggers in SQL Server. Start working on it and let us know if you have any issues.
DECLARE #tt TABLE (id INT NOT NULL PRIMARY KEY, value NUMERIC(10, 0) NOT NULL)
INSERT
INTO #tt
VALUES (1, 3.1415926)
SELECT *
FROM #tt
, without any triggers.