Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Help Groups for Snitz Forums 2000 Users
 Help: Database: MS SQL Server
 Changing Identity Insert Problem
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 3

Podge
Support Moderator

Ireland
3775 Posts

Posted - 05 September 2005 :  19:39:29  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message

In query analyser this works (but I cannot use it in this format)

SET IDENTITY_INSERT [FORUM_AVATAR] ON

but these don't

exec 'SET IDENTITY_INSERT [FORUM_AVATAR] ON'
execute sp_executesql N'SET IDENTITY_INSERT [FORUM_AVATAR] ON'

Identity Insert remains 'OFF' though I get a message saying that the command completed successfully.

Any ideas?

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 05 September 2005 :  20:37:15  Show Profile  Send ruirib a Yahoo! Message
You mean the EXEC statements don't work in Query Analiser?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 06 September 2005 :  05:28:30  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
They appear to work as I get "command completed successfully" but in reality it doesn't do anything. If I try to insert a row (containing the row ID) I get a error stating that identity insert is off.

The second two examples don't work in a stored procedure either which is where I really want to use it. I'm passing the table name as a parameter and using it to build a sql statement (which is why I cannot use the first example) e.g.

this works in the stored procedure & query analyser

exec ('select * from ' + @tablename + ')

but this doesn't

exec 'SET IDENTITY_INSERT [' + @tablename + '] ON'

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  05:32:25  Show Profile  Send ruirib a Yahoo! Message
I tried each of the situations in Query Analizer and had no problems doing it. I did inserts specifying the ID value and it all worked.

Haven't tested in a stored proc, but I can give it a go. What Service Pack have you installed?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 06 September 2005 :  05:53:40  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Tried it on MSDE SP3 and MSSQL Standard SP4.

I'll post a partial SP in a minute. Can you test it to see if it works?

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  05:56:01  Show Profile  Send ruirib a Yahoo! Message
Sure.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  05:58:43  Show Profile  Send ruirib a Yahoo! Message
I tested your previous stuff with SQL Server Developer SP4 and it worked.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 06 September 2005 :  06:09:05  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Assume that tablename and database name are exactly the same

CREATE PROCEDURE [DBO].[move_table]

@tablename varchar(100)

as

exec ('SET IDENTITY_INSERT [snitzforum].[dbo].[' + @tablename +'_AVATAR] ON')

exec ('INSERT INTO [snitzforum].[dbo].[' + @tablename + '_AVATAR]([A_ID], [A_URL], [A_NAME], [A_MEMBER_ID]) SELECT [A_ID], [A_URL], [A_NAME], [A_MEMBER_ID] FROM [' + @tablename + '].[dbo].[' + @tablename + '_AVATAR]')

exec ('SET IDENTITY_INSERT [snitzforum].[dbo].[' + @tablename +'_AVATAR] OFF')

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  06:57:30  Show Profile  Send ruirib a Yahoo! Message
I get an error message stating that the table cannot be open for SET operation.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 06 September 2005 :  07:13:09  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
That usually happens if the table doesn't exist. I got it once or twice but I had made a mistake with the table name. After I resolved it I got the identity insert error.

I know you probably know this already but;

You have to create a database called snitzforum
Add the above stored procedure to it.
Run the avatar dbs file having demo_ (for example) as the prefix for the table name.
Create a database called demo
Run the avatar dbs file having demo_ as the prefix for the table name
Put some sample data in this table
Run the stored procedure

exec [snitzforum].[dbo].[move_table] 'demo'


Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  07:14:09  Show Profile  Send ruirib a Yahoo! Message
Ok, first problem solved, but now I'm experiencing your situation as well: a message stating that IDENTITY_INSERT is OFF.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  07:17:05  Show Profile  Send ruirib a Yahoo! Message
It's the exec thing. If you try to set the IDENTITY_INSERT outside the exec it works. Now we need to find out if this can be overcome.

Stupid thing, the code works in Query Analizer...
No, it does not work either.


Snitz 3.4 Readme | Like the support? Support Snitz too

Edited by - ruirib on 06 September 2005 07:18:45
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  07:26:46  Show Profile  Send ruirib a Yahoo! Message
I think I've found it. The problem is that the EXEC runs in its own batch. You need to execute all the statements in a single batch:

CREATE PROCEDURE [DBO].[move_table]

@tablename varchar(100)

as

exec ('SET IDENTITY_INSERT [snitzforum].[dbo].[' + @tablename +'_AVATAR] ON; INSERT INTO [snitzforum].[dbo].[' + @tablename + '_AVATAR]([A_ID], [A_URL], [A_NAME], [A_MEMBER_ID]) SELECT [A_ID], [A_URL], [A_NAME], [A_MEMBER_ID] FROM [' + @tablename + '].[dbo].[' + @tablename + '_AVATAR];SET IDENTITY_INSERT [snitzforum].[dbo].[' + @tablename +'_AVATAR] OFF')

This should work. It works with query analizer and as a stored proc.


Snitz 3.4 Readme | Like the support? Support Snitz too

Edited by - ruirib on 06 September 2005 07:28:24
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 06 September 2005 :  07:44:34  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
I thought it could be something like that. I saw something in BOL last night like this

use master
go
exec("use pubs") select * from authors

After executing the above the context is still the master database.

I actually tried code similar to yours last night but without the ; between the statements. I'll try it later.

Thanks for the help. Again!

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 September 2005 :  07:48:31  Show Profile  Send ruirib a Yahoo! Message
Ok, let me know how it goes.
Worked for me, though I had no records in the database I used for testing, but the error message went away, so I expect it to work for you as well.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 06 September 2005 :  07:53:24  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
One observation;

Its going to make for an ugly looking sp unless I can set my screen resolution to 5120x3840

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page
Page: of 3 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.22 seconds. Powered By: Snitz Forums 2000 Version 3.4.07