Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have successfully imported database from msaccess to sql server using linked servers but it doesn't set is identity prpoerty to columns in destination table.

I have written one stored procedure with setting the identity_insert on before the select * into query but it is being gotten error.

This is my procedure


SQL
create proc 
SP_Create(@servername nvarchar(100),
@datasr nvarchar(MAX),@table nvarchar(100),
@table1 nvarchar(100),@db nvarchar(100)) 
as begin EXEC sp_addlinkedserver @server = @servername, 
@provider = 'Microsoft.Jet.OLEDB.4.0', 
@srvproduct = 'OLE DB Provider for Jet',
@datasrc = @datasr,@provstr=''
exec sp_addlinkedsrvlogin @rmtsrvname=@servername, 
@useself = N'false',@locallogin = NULL,@rmtuser = N'Admin',
@rmtpassword = NULL declare @sql nvarchar(MAX) 
set @sql=' SET IDENTITY_INSERT '+@table1+' ON; select * into '+@table1+' from '+@servername+'...'+@table+' with (nolock) SET IDENTITY_INSERT '+@table1+' ON '
Exec(@sql) 
declare @sql1 nvarchar(MAX) set @sql1 ='EXEC master.sys.sp_dropserver '+@servername+',''droplogins''' exec(@sql1)
end
Posted
Comments
Richard Deeming 14-Oct-15 9:26am    
And are we supposed to guess what the error is?
TarunKumarSusarapu 14-Oct-15 23:55pm    
That table does not exist or you don't need permissions Because here i have used select * into query so we can copy the table with schema

1 solution

You might need to provide a more complete example.

Perhaps you are seeing an error message when your stored procedure runs, at the time the SET IDENTITY statement is executed, your new table (table1) does not yet exist. The new table (table1) is created by the SELECT INTO statement.

For example, if you execute this SQL in a standalone SQL Query window, it returns the correct expected results:

SQL
CREATE TABLE A ( ID INT NOT NULL IDENTITY PRIMARY KEY, Name NVARCHAR(16) NOT NULL )
INSERT INTO A ( Name ) SELECT 'X' UNION SELECT 'Y'
INSERT INTO A ( Name ) SELECT 'A' UNION SELECT 'B'
DELETE FROM A WHERE Name = 'A'
SELECT * INTO B FROM A WITH (NOLOCK)
SELECT * FROM B
-- Correctly returns {(1,X);(2,Y);(4,B)}
 
Share this answer
 
Comments
TarunKumarSusarapu 20-Oct-15 0:56am    
Here we are copying total database that contains 1000 tables from msaccess mdb files to sql server db. For every table we can't give identity and create one table that table copied into another table it takes a lot of time. Would u pls explain this query briefly
Daniel Miller 20-Oct-15 10:33am    
You don't need to create the destination tables, and you don't need to worry about setting IDENTITY_INSERT in your stored procedure.

"SELECT * INTO" will create the table and correctly load your data from the source tables.

I don't know what error you are getting, but I think you should remove "SET IDENTITY_INSERT '+@table1+' ON;" from the code in your stored procedure because that is the most likely source for an error.
TarunKumarSusarapu 21-Oct-15 2:24am    
Here I am using Linked server to retrieve data from msaccess database and then by using linkserver i am copying the total database tables from msaccess to sql server.Here all the tables copied but the is identity property is missing i.e it set to NO
Daniel Miller 21-Oct-15 10:05am    
To confirm then, you are no longer getting an error, and the tables in SQL Server are successfully created and loaded from Access, but the primary key column in the new tables does not have the Identity property enabled - is that correct?

If so, then this is a resource you might want to check:
http://blog.sqlauthority.com/2009/05/03/sql-server-add-or-remove-identity-property-on-column/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900