Showing posts with label triggers. Show all posts
Showing posts with label triggers. Show all posts

Friday, March 30, 2012

Scripting ALTER TABLE

I need to create a script to disable all triggers and constraints in my
database.

It appears as though I cannot use a local variable for the table name in the
ALTER TABLE statement (e.g. ALTER TABLE @.TBL).

Is there any reason for this?

Thanks,

Kevin"Kevin Haugen" <khaugen@.pacbell.net> wrote in message
news:jx1Hd.12736$5R.1377@.newssvr21.news.prodigy.co m...
>I need to create a script to disable all triggers and constraints in my
>database.
> It appears as though I cannot use a local variable for the table name in
> the ALTER TABLE statement (e.g. ALTER TABLE @.TBL).
> Is there any reason for this?
> Thanks,
> Kevin

You can't use variables in place of table or column names, except when using
dynamic SQL, which has its own issues. Although if you're a DBA running an
admin script, then it's usually a reasonable option - there's more
discussion here:

http://www.sommarskog.se/dynamic_sql.html

Unfortunately, you don't say what your goal is, but if it's to load data
into the database, then all the usual loading tools (bcp.exe, DTS, BULK
INSERT) can ignore both constraints and triggers, so you might not need a
script anyway.

Simon|||"Simon Hayes" <sql@.hayes.ch> wrote in message
news:41ecd751$1_1@.news.bluewin.ch...
> "Kevin Haugen" <khaugen@.pacbell.net> wrote in message
> news:jx1Hd.12736$5R.1377@.newssvr21.news.prodigy.co m...
>>I need to create a script to disable all triggers and constraints in my
>>database.
>>
>> It appears as though I cannot use a local variable for the table name in
>> the ALTER TABLE statement (e.g. ALTER TABLE @.TBL).
>>
>> Is there any reason for this?
>>
>> Thanks,
>>
>> Kevin
>>
> You can't use variables in place of table or column names, except when
> using dynamic SQL, which has its own issues. Although if you're a DBA
> running an admin script, then it's usually a reasonable option - there's
> more discussion here:
> http://www.sommarskog.se/dynamic_sql.html
> Unfortunately, you don't say what your goal is, but if it's to load data
> into the database, then all the usual loading tools (bcp.exe, DTS, BULK
> INSERT) can ignore both constraints and triggers, so you might not need a
> script anyway.
> Simon

I'll look into it. I'm planning on converting existing data into a new
format. Since I have to identify each table and write a query to do the
conversion, I could easily do a copy/paste for each table to disable and
re-enable the triggers and constraints. I am hoping to shortcut some of the
work by automating that piece.

Thanks,

Kevin

Tuesday, March 20, 2012

Script only triggers in a database

Is there a way to script only the triggers in
a database?
1. Right Click on the database name and select All Tasks and underneth that select Generate SQL Script. You will be presented with Generate SQL Scripts dialog box.
2. In the General Tab Click 'Show All' button. Then select 'All Tables' check box.
3. In the formatting Tab of the dialog box, delete everything 'Script template' text area.
4. In the Options tab, under table scripting options check 'Script triggers' box.
The main trick is to delete everything in the Formatting tab of the dialog box.
|||Using Enterprise Manager you can select a database, right click and choose
All Tasks>Generate SQL Script. Then select Show All and check the All Tables
box. Next choose the Formatting Tab and uncheck all boxes. On the Options
tab, check the Script Triggers checkbox in the Table Scripting options
section. This should now generate the script for all the triggers. You can
also use SQLDMO in a VBScript file e.g.
Set oSQL = WScript.CreateObject("SQLDMO.SQLServer")
oSQL.Name = "(local)"
oSQL.LoginSecure = True
oSQL.LoginTimeout = 10
oSQL.Connect
Set oTransfer = WScript.CreateObject("SQLDMO.Transfer")
oTransfer.CopyAllTriggers = True
Set oDB = oSQL.Databases("pubs")
oDB.ScriptTransfer oTransfer,2,"c:\pubs_triggers.sql"
oSQL.DisConnect
Set oTransfer = Nothing
Set oSQL = Nothing
HTH
Jasper Smith (SQL Server MVP)
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Phil396" <anonymous@.discussions.microsoft.com> wrote in message
news:16e8f01c420b5$0dddb6e0$a001280a@.phx.gbl...
> Is there a way to script only the triggers in
> a database?

Script only triggers in a database

Is there a way to script only the triggers in
a database?1. Right Click on the database name and select All Tasks and underneth that select Generate SQL Script. You will be presented with Generate SQL Scripts dialog box
2. In the General Tab Click 'Show All' button. Then select 'All Tables' check box
3. In the formatting Tab of the dialog box, delete everything 'Script template' text area
4. In the Options tab, under table scripting options check 'Script triggers' box
The main trick is to delete everything in the Formatting tab of the dialog box.|||Using Enterprise Manager you can select a database, right click and choose
All Tasks>Generate SQL Script. Then select Show All and check the All Tables
box. Next choose the Formatting Tab and uncheck all boxes. On the Options
tab, check the Script Triggers checkbox in the Table Scripting options
section. This should now generate the script for all the triggers. You can
also use SQLDMO in a VBScript file e.g.
Set oSQL = WScript.CreateObject("SQLDMO.SQLServer")
oSQL.Name = "(local)"
oSQL.LoginSecure = True
oSQL.LoginTimeout = 10
oSQL.Connect
Set oTransfer = WScript.CreateObject("SQLDMO.Transfer")
oTransfer.CopyAllTriggers = True
Set oDB = oSQL.Databases("pubs")
oDB.ScriptTransfer oTransfer,2,"c:\pubs_triggers.sql"
oSQL.DisConnect
Set oTransfer = Nothing
Set oSQL = Nothing
HTH
Jasper Smith (SQL Server MVP)
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Phil396" <anonymous@.discussions.microsoft.com> wrote in message
news:16e8f01c420b5$0dddb6e0$a001280a@.phx.gbl...
> Is there a way to script only the triggers in
> a database?

Script only triggers in a database

Is there a way to script only the triggers in
a database?1. Right Click on the database name and select All Tasks and underneth that
select Generate SQL Script. You will be presented with Generate SQL Scripts
dialog box.
2. In the General Tab Click 'Show All' button. Then select 'All Tables' chec
k box.
3. In the formatting Tab of the dialog box, delete everything 'Script templa
te' text area.
4. In the Options tab, under table scripting options check 'Script triggers'
box.
The main trick is to delete everything in the Formatting tab of the dialog b
ox.|||Using Enterprise Manager you can select a database, right click and choose
All Tasks>Generate SQL Script. Then select Show All and check the All Tables
box. Next choose the Formatting Tab and uncheck all boxes. On the Options
tab, check the Script Triggers checkbox in the Table Scripting options
section. This should now generate the script for all the triggers. You can
also use SQLDMO in a VBScript file e.g.
Set oSQL = WScript.CreateObject("SQLDMO.SQLServer")
oSQL.Name = "(local)"
oSQL.LoginSecure = True
oSQL.LoginTimeout = 10
oSQL.Connect
Set oTransfer = WScript.CreateObject("SQLDMO.Transfer")
oTransfer.CopyAllTriggers = True
Set oDB = oSQL.Databases("pubs")
oDB.ScriptTransfer oTransfer,2,"c:\pubs_triggers.sql"
oSQL.DisConnect
Set oTransfer = Nothing
Set oSQL = Nothing
HTH
Jasper Smith (SQL Server MVP)
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Phil396" <anonymous@.discussions.microsoft.com> wrote in message
news:16e8f01c420b5$0dddb6e0$a001280a@.phx
.gbl...
> Is there a way to script only the triggers in
> a database?

Monday, March 12, 2012

script for trace form Query analyzer ?

I want to know which procs and triggers that are taking a lot of time to run. I cannot run the trace coz its slowing down. There are lot of procs and triggers written over several years.
TIA
Are you saying that Profiler is resulting in performance degradation? On the
client or server?
You might want to trace using system stored procedures, on the server side.
This is much better than running Profiler interactively.
For more info:
http://vyaskn.tripod.com/server_side...sql_server.htm
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
news:DB0143FE-769C-486D-8473-EA2CD95DBE17@.microsoft.com...
I want to know which procs and triggers that are taking a lot of time to
run. I cannot run the trace coz its slowing down. There are lot of procs
and triggers written over several years.
TIA
|||Do you by any chance know which systables can I look up to get the same info as from the server procs.
or else by any chance do you know how to look at the script contained in the below procs
sp_trace_create
sp_trace_generateevent
sp_trace_setevent
sp_trace_setfilter
sp_trace_setstatus
fn_trace_getfilterinfo
fn_trace_getinfo Returns
fn_trace_gettable Returns
thanks !!
"Narayana Vyas Kondreddi" wrote:

> Are you saying that Profiler is resulting in performance degradation? On the
> client or server?
> You might want to trace using system stored procedures, on the server side.
> This is much better than running Profiler interactively.
> For more info:
> http://vyaskn.tripod.com/server_side...sql_server.htm
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> Is .NET important for a database professional?
> http://vyaskn.tripod.com/poll.htm
>
> "DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
> news:DB0143FE-769C-486D-8473-EA2CD95DBE17@.microsoft.com...
> I want to know which procs and triggers that are taking a lot of time to
> run. I cannot run the trace coz its slowing down. There are lot of procs
> and triggers written over several years.
> TIA
>
>
|||This info is not stored in any of the system tables.
I think the server side trace stored procs are implemented as extended
stored procs, so the source code is not available
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
news:AA76511D-FD1C-4670-B083-78FF5A3F65E7@.microsoft.com...
Do you by any chance know which systables can I look up to get the same info
as from the server procs.
or else by any chance do you know how to look at the script contained in the
below procs
sp_trace_create
sp_trace_generateevent
sp_trace_setevent
sp_trace_setfilter
sp_trace_setstatus
fn_trace_getfilterinfo
fn_trace_getinfo Returns
fn_trace_gettable Returns
thanks !!
"Narayana Vyas Kondreddi" wrote:

> Are you saying that Profiler is resulting in performance degradation? On
the
> client or server?
> You might want to trace using system stored procedures, on the server
side.
> This is much better than running Profiler interactively.
> For more info:
> http://vyaskn.tripod.com/server_side...sql_server.htm
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> Is .NET important for a database professional?
> http://vyaskn.tripod.com/poll.htm
>
> "DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
> news:DB0143FE-769C-486D-8473-EA2CD95DBE17@.microsoft.com...
> I want to know which procs and triggers that are taking a lot of time to
> run. I cannot run the trace coz its slowing down. There are lot of procs
> and triggers written over several years.
> TIA
>
>

script for trace form Query analyzer ?

I want to know which procs and triggers that are taking a lot of time to run
. I cannot run the trace coz its slowing down. There are lot of procs and t
riggers written over several years.
TIAAre you saying that Profiler is resulting in performance degradation? On the
client or server?
You might want to trace using system stored procedures, on the server side.
This is much better than running Profiler interactively.
For more info:
http://vyaskn.tripod.com/server_sid..._sql_server.htm
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
news:DB0143FE-769C-486D-8473-EA2CD95DBE17@.microsoft.com...
I want to know which procs and triggers that are taking a lot of time to
run. I cannot run the trace coz its slowing down. There are lot of procs
and triggers written over several years.
TIA|||Do you by any chance know which systables can I look up to get the same info
as from the server procs.
or else by any chance do you know how to look at the script contained in the
below procs
sp_trace_create
sp_trace_generateevent
sp_trace_setevent
sp_trace_setfilter
sp_trace_setstatus
fn_trace_getfilterinfo
fn_trace_getinfo Returns
fn_trace_gettable Returns
thanks !!
"Narayana Vyas Kondreddi" wrote:

> Are you saying that Profiler is resulting in performance degradation? On t
he
> client or server?
> You might want to trace using system stored procedures, on the server side
.
> This is much better than running Profiler interactively.
> For more info:
> http://vyaskn.tripod.com/server_sid..._sql_server.htm
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> Is .NET important for a database professional?
> http://vyaskn.tripod.com/poll.htm
>
> "DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
> news:DB0143FE-769C-486D-8473-EA2CD95DBE17@.microsoft.com...
> I want to know which procs and triggers that are taking a lot of time to
> run. I cannot run the trace coz its slowing down. There are lot of procs
> and triggers written over several years.
> TIA
>
>|||This info is not stored in any of the system tables.
I think the server side trace stored procs are implemented as extended
stored procs, so the source code is not available
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
news:AA76511D-FD1C-4670-B083-78FF5A3F65E7@.microsoft.com...
Do you by any chance know which systables can I look up to get the same info
as from the server procs.
or else by any chance do you know how to look at the script contained in the
below procs
sp_trace_create
sp_trace_generateevent
sp_trace_setevent
sp_trace_setfilter
sp_trace_setstatus
fn_trace_getfilterinfo
fn_trace_getinfo Returns
fn_trace_gettable Returns
thanks !!
"Narayana Vyas Kondreddi" wrote:

> Are you saying that Profiler is resulting in performance degradation? On
the
> client or server?
> You might want to trace using system stored procedures, on the server
side.
> This is much better than running Profiler interactively.
> For more info:
> http://vyaskn.tripod.com/server_sid..._sql_server.htm
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> Is .NET important for a database professional?
> http://vyaskn.tripod.com/poll.htm
>
> "DallasBlue" <DallasBlue@.discussions.microsoft.com> wrote in message
> news:DB0143FE-769C-486D-8473-EA2CD95DBE17@.microsoft.com...
> I want to know which procs and triggers that are taking a lot of time to
> run. I cannot run the trace coz its slowing down. There are lot of procs
> and triggers written over several years.
> TIA
>
>

Tuesday, February 21, 2012

SCOPE_IDENTITY() vs. @@IDENTITY

I have a basic C# application that is trying to INSERT a row and get the ID. Really simple; there are no triggers, no stored procs or functions were involved, the app is single threaded, there is currently only one user. I have a really basic table with a INTEGER IDENTITY PK column. All very standard.

If I do the INSERT followed by a "SELECT @.@.IDENTITY" on the same connection, it works correctly.
If I use SCOPE_IDENTITY() instead, it returns NULL. I use SCOPE_IDENTITY on a variety of other occassions and it works fine. Why would this be? I thought SCOPE_IDENTITY() was the preferred replacement to @.@.IDENTITY.

I guess what I have is satisfactory but this was frustrating and I want to know why.

This is on SQL Server 2000 Standard edition with version SP3a + hot fixeshai roger,

SCOPE_IDENTITY and @.@.IDENTITY will return last identity values generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope. @.@.IDENTITY is not limited to a specific scope.

This is the example given in BOL

Suppose if you have two tables, T1 and T2, and an INSERT trigger defined on T1. When a row is inserted to T1, the trigger fires and inserts a row in T2. This scenario illustrates two scopes: the insert on T1, and the insert on T2 as a result of the trigger.

Assuming that both T1 and T2 have IDENTITY columns, @.@.IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on T1.

@.@.IDENTITY will return the last IDENTITY column value inserted across any scope in the current session, which is the value inserted in T2.

SCOPE_IDENTITY() will return the IDENTITY value inserted in T1, which was the last INSERT that occurred in the same scope. The SCOPE_IDENTITY() function will return the NULL value if the function is invoked before any insert statements into an identity column occur in the scope.

Hope this relives u of ur frustration|||Yes, I read BOL and understand the documented theoretical and conceptual differences between the two. However, those differences don't apply to my situation.

There is only one thread, one process, one identity value, and one table involved. There are no triggers or functions or stored procs involved. It's a simple INSERT/get ID situation. And @.@.IDENTITY works and SCOPE_IDENTITY doesn't work which just doesn't make any sense according to what I've read.

SCOPE_IDENTITY() and "instead of" Triggers.

Here's a fun question :)
If I have an instead of trigger on a table, which replaces the insert, how
can I get the identity insert from the data inserted?
-- example --
IF OBJECT_ID('dbo.tblTest') IS NOT NULL DROP TABLE dbo.tblTest
CREATE TABLE dbo.tblTest ( ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY
CLUSTERED , Data1 CHAR(1) NOT NULL )
INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'A' )
PRINT 'T-SQL: B ->' + CAST( SCOPE_IDENTITY() AS VARCHAR(11)) -- 1
GO
CREATE TRIGGER
TR_dbo_tblTest
ON
dbo.tblTest
INSTEAD OF INSERT
AS
SET NOCOUNT ON
IF ( ( SELECT COUNT(*) FROM inserted ) > 0 )
BEGIN
INSERT INTO
dbo.tblTest ( Data1 )
SELECT
Data1
FROM
inserted
PRINT 'TR_dbo_tblTest ->' + CAST( SCOPE_IDENTITY() AS VARCHAR(11)) -- 2
END
GO
INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'B' )
-- The following should return 2, but it returns NULL because the insert
was done by the trigger.
PRINT 'T-SQL: B ->' + ISNULL( CAST( SCOPE_IDENTITY() AS VARCHAR(11)) ,
'<NULL>' ) -- NULLI could be wrong, but are trying to get the ID for the record inserted?
If you are... When I use Int Identity fields for the primary key, I use a SP
to insert my records when I need to know what the primary key is.
For example, this SP is used to insert a new record into the Contacts table.
It returns a result set with a field called NewID that contains the ID for
the new record.
Create Procedure [dbo].[NewContactRec_SP]
@.LName VARCHAR(30),
@.FName VARCHAR(20),
@.ResID Int,
@.StaffID Int
as
Insert Into Contacts
(LName,FName,ResID,CreatedStaffID)
Values (@.LName,@.Fname,@.ResID,@.StaffID)
/* Return New ID */
Select SCOPE_IDENTITY() As NewID
HTH,
-Steve-|||For INSTEAD OF triggers, use the ol' @.@.IDENTITY instead:
INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'B' )
PRINT 'T-SQL: B ->' + ISNULL( CAST( @.@.IDENTITY AS VARCHAR(11)) ,'<NULL>' )
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Rebecca York" <rebecca.york {at} 2ndbyte.com> wrote in message
news:43551654$0$135$7b0f0fd3@.mistral.news.newnet.co.uk...
> Here's a fun question :)
> If I have an instead of trigger on a table, which replaces the insert, how
> can I get the identity insert from the data inserted?
>
> -- example --
> IF OBJECT_ID('dbo.tblTest') IS NOT NULL DROP TABLE dbo.tblTest
> CREATE TABLE dbo.tblTest ( ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY
> CLUSTERED , Data1 CHAR(1) NOT NULL )
> INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'A' )
> PRINT 'T-SQL: B ->' + CAST( SCOPE_IDENTITY() AS VARCHAR(11)) -- 1
>
> GO
> CREATE TRIGGER
> TR_dbo_tblTest
> ON
> dbo.tblTest
> INSTEAD OF INSERT
> AS
> SET NOCOUNT ON
> IF ( ( SELECT COUNT(*) FROM inserted ) > 0 )
> BEGIN
> INSERT INTO
> dbo.tblTest ( Data1 )
> SELECT
> Data1
> FROM
> inserted
> PRINT 'TR_dbo_tblTest ->' + CAST( SCOPE_IDENTITY() AS VARCHAR(11)) -- 2
> END
> GO
>
> INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'B' )
> -- The following should return 2, but it returns NULL because the insert
> was done by the trigger.
> PRINT 'T-SQL: B ->' + ISNULL( CAST( SCOPE_IDENTITY() AS VARCHAR(11)) ,
> '<NULL>' ) -- NULL
>|||We have a ton of auditing code inside the trigger and can't change it to a
stored proc, mainly because people can still edit the data in SQL-EM and
these changes still need to be audited.
"Steve Zimmelman" <skz@.charter.nospam.net> wrote in message
news:eEstw$$0FHA.3660@.TK2MSFTNGP15.phx.gbl...
> I could be wrong, but are trying to get the ID for the record inserted?
> If you are... When I use Int Identity fields for the primary key, I use a
SP
> to insert my records when I need to know what the primary key is.
> For example, this SP is used to insert a new record into the Contacts
table.
> It returns a result set with a field called NewID that contains the ID for
> the new record.
> Create Procedure [dbo].[NewContactRec_SP]
> @.LName VARCHAR(30),
> @.FName VARCHAR(20),
> @.ResID Int,
> @.StaffID Int
> as
> Insert Into Contacts
> (LName,FName,ResID,CreatedStaffID)
> Values (@.LName,@.Fname,@.ResID,@.StaffID)
> /* Return New ID */
> Select SCOPE_IDENTITY() As NewID
> HTH,
> -Steve-
>|||Ok,
But that requires an exclusive table lock to stop people putting in more
data :)
I tried SET TRANSACTION ISOLATION LEVEL SERIALIZABLE,
but this still allowed multiple transactions to insert data
Good 'ol WITH(TABLOCKX) :)
Unless there's another way to block inserts, without blocking everything
else?
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:uUnggGA1FHA.268@.TK2MSFTNGP09.phx.gbl...
> For INSTEAD OF triggers, use the ol' @.@.IDENTITY instead:
> INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'B' )
> PRINT 'T-SQL: B ->' + ISNULL( CAST( @.@.IDENTITY AS VARCHAR(11)) ,'<NULL>' )
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Rebecca York" <rebecca.york {at} 2ndbyte.com> wrote in message
> news:43551654$0$135$7b0f0fd3@.mistral.news.newnet.co.uk...
how
insert
>|||> But that requires an exclusive table lock to stop people putting in more
> data :)
Hmm, not sure I understand. Why are you saying that?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Rebecca York" <rebecca.york {at} 2ndbyte.com> wrote in message
news:43551dbd$0$142$7b0f0fd3@.mistral.news.newnet.co.uk...
> Ok,
> But that requires an exclusive table lock to stop people putting in more
> data :)
> I tried SET TRANSACTION ISOLATION LEVEL SERIALIZABLE,
> but this still allowed multiple transactions to insert data
> Good 'ol WITH(TABLOCKX) :)
> Unless there's another way to block inserts, without blocking everything
> else?
>
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote i
n
> message news:uUnggGA1FHA.268@.TK2MSFTNGP09.phx.gbl...
> how
> insert
>|||Yeah, this stinks. Like Tibor says, use @.@.identity if you can (it does not
require locks, it is scoped to a single session.
The best thing to do is to use your other key (you should have one because
the surrogate key (the identity value) should be a surrogate for something,
otherwise you have a potential mess) to fetch the value:
insert into dbo.tblTest --probably stop prefixing tables with tbl too :)
values...
select ID -- generally better to name <tablename>Id so they are easier to
implement/use as foreign keys
from tblTest
where keycolumn<s> = @.valueYouEntered
If you want it changed in the future, please go to:
http://lab.msdn.microsoft.com/produ...1b29351&lc=1033
And vote for this!
Thanks!
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Rebecca York" <rebecca.york {at} 2ndbyte.com> wrote in message
news:43551654$0$135$7b0f0fd3@.mistral.news.newnet.co.uk...
> Here's a fun question :)
> If I have an instead of trigger on a table, which replaces the insert, how
> can I get the identity insert from the data inserted?
>
> -- example --
> IF OBJECT_ID('dbo.tblTest') IS NOT NULL DROP TABLE dbo.tblTest
> CREATE TABLE dbo.tblTest ( ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY
> CLUSTERED , Data1 CHAR(1) NOT NULL )
> INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'A' )
> PRINT 'T-SQL: B ->' + CAST( SCOPE_IDENTITY() AS VARCHAR(11)) -- 1
>
> GO
> CREATE TRIGGER
> TR_dbo_tblTest
> ON
> dbo.tblTest
> INSTEAD OF INSERT
> AS
> SET NOCOUNT ON
> IF ( ( SELECT COUNT(*) FROM inserted ) > 0 )
> BEGIN
> INSERT INTO
> dbo.tblTest ( Data1 )
> SELECT
> Data1
> FROM
> inserted
> PRINT 'TR_dbo_tblTest ->' + CAST( SCOPE_IDENTITY() AS VARCHAR(11)) -- 2
> END
> GO
>
> INSERT INTO dbo.tblTest ( Data1 ) VALUES ( 'B' )
> -- The following should return 2, but it returns NULL because the insert
> was done by the trigger.
> PRINT 'T-SQL: B ->' + ISNULL( CAST( SCOPE_IDENTITY() AS VARCHAR(11)) ,
> '<NULL>' ) -- NULL
>|||> Yeah, this stinks. Like Tibor says, use @.@.identity if you can (it does
> not require locks, it is scoped to a single session.
Oh yeah, the reason why you might not be able to use this would be if a
trigger inserted data into another table.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Louis Davidson" <dr_dontspamme_sql@.hotmail.com> wrote in message
news:OyyqtiB1FHA.2884@.TK2MSFTNGP09.phx.gbl...
> Yeah, this stinks. Like Tibor says, use @.@.identity if you can (it does
> not require locks, it is scoped to a single session.
> The best thing to do is to use your other key (you should have one because
> the surrogate key (the identity value) should be a surrogate for
> something, otherwise you have a potential mess) to fetch the value:
> insert into dbo.tblTest --probably stop prefixing tables with tbl too :)
> values...
> select ID -- generally better to name <tablename>Id so they are easier to
> implement/use as foreign keys
> from tblTest
> where keycolumn<s> = @.valueYouEntered
> If you want it changed in the future, please go to:
> http://lab.msdn.microsoft.com/produ...1b29351&lc=1033
> And vote for this!
> Thanks!
> --
> ----
--
> Louis Davidson - http://spaces.msn.com/members/drsql/
> SQL Server MVP
> "Arguments are to be avoided: they are always vulgar and often
> convincing." (Oscar Wilde)
> "Rebecca York" <rebecca.york {at} 2ndbyte.com> wrote in message
> news:43551654$0$135$7b0f0fd3@.mistral.news.newnet.co.uk...
>|||Yeah I just remembered,
The auditing tables also have an identity field :)
fun \o/
"Louis Davidson" <dr_dontspamme_sql@.hotmail.com> wrote in message
news:e4ckAmB1FHA.3956@.TK2MSFTNGP09.phx.gbl...
> Oh yeah, the reason why you might not be able to use this would be if a
> trigger inserted data into another table.
> --
> ----
--
> Louis Davidson - http://spaces.msn.com/members/drsql/
> SQL Server MVP
> "Arguments are to be avoided: they are always vulgar and often
convincing."
> (Oscar Wilde)
> "Louis Davidson" <dr_dontspamme_sql@.hotmail.com> wrote in message
> news:OyyqtiB1FHA.2884@.TK2MSFTNGP09.phx.gbl...
because
to
http://lab.msdn.microsoft.com/produ...1b29351&lc=1033
> ----
--
2
insert
>|||Yes you're right of course it's session based,
I've been playing with mysql too much at the wend :o
"Louis Davidson" <dr_dontspamme_sql@.hotmail.com> wrote in message
news:OyyqtiB1FHA.2884@.TK2MSFTNGP09.phx.gbl...
> Yeah, this stinks. Like Tibor says, use @.@.identity if you can (it does
not
> require locks, it is scoped to a single session.