Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Copy and paste except one column of a table

Author  Topic 

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2013-06-01 : 08:19:02
Hello all..please help.

I have a table like

id name age
01 aaa 22
01 bbb 33
01 ccc 44

Now i need to add few data to the table below and make it look like

id name age
01 aaa 22
01 bbb 33
01 ccc 44
02 aaa 22
02 bbb 33
02 ccc 44

not sure how to get

i used insert into .............select from

but did not work??




Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-01 : 20:18:00
if your attempt is to replicate the values for id=01 you can use this

INSERT INTO Table
SELECT '02',name,age
FROM Table
WHERE id = '01'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2013-06-03 : 00:35:06
Thanks Visakh,

But my intention was not to replicate. but to get the data below 01 ID.

It works like INSERT TO.... SELECT FROM..

but not sure how to get that???



Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-03 : 02:12:56
quote:
Originally posted by anuraag205

Thanks Visakh,

But my intention was not to replicate. but to get the data below 01 ID.

It works like INSERT TO.... SELECT FROM..

but not sure how to get that???



Thanks


its exactly like what i showed you
did you try it?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-03 : 02:13:27
quote:
Originally posted by anuraag205

Thanks Visakh,

But my intention was not to replicate. but to get the data below 01 ID.

It works like INSERT TO.... SELECT FROM..

but not sure how to get that???



Thanks


its exactly like what i showed you
did you try it?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2013-06-03 : 03:22:56
Yes VIsakh,

------------------
INSERT INTO [tblsupplierlocation]
SELECT [pk_SupplierLocationID],[423],[fk_CountryID],[fk_LocationID],[IsAirport],[IsMeetnGreet],
[IsDeskInTermainl],[Description],[IsShutOnHolidays],[Address1],[Address2],[Address3],[City],[State],
[PostCode],[ContactEmailID],[ContactPhone],[PublicNote],[PrivateNote],[WorkingHoursSummary],
[IsShowOnWebSite],[IsActive],[IsDeleted],[CreateUserID],[CreateDateTime],[UpdateUserID],[UpdatedDateTime]
FROM [tblsupplierlocation]
WHERE [fk_SupplierID]in (414)

----------------------------------------------------

and the error shows "Msg 207, Level 16, State 1, Line 2
Invalid column name '423'.
Msg 8101, Level 16, State 1, Line 2
An explicit value for the identity column in table 'tblsupplierlocation' can only be specified when a column list is used and IDENTITY_INSERT is ON.
---------------------------------------------------------

So could not guess where the error lies...
Sorry yaar

Thanks
Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2013-06-03 : 03:33:25
I also tried to update from excel

INSERT INTO [tblsupplierlocation]
([pk_SupplierLocationID],[fk_SupplierID],[fk_CountryID],[fk_LocationID],[IsAirport],[IsMeetnGreet],
[IsDeskInTermainl],[Description],[IsShutOnHolidays],[Address1],[Address2],[Address3],[City],[State],
[PostCode],[ContactEmailID],[ContactPhone],[PublicNote],[PrivateNote],[WorkingHoursSummary],
[IsShowOnWebSite],[IsActive],[IsDeleted],[CreateUserID],[CreateDateTime],[UpdateUserID],[UpdatedDateTime])

SELECT A.[pk_SupplierLocationID],
A.[fk_SupplierID],
A.[fk_CountryID],A.[fk_LocationID],A.[IsAirport],A.[IsMeetnGreet],
A.[IsDeskInTermainl],A.[Description],A.[IsShutOnHolidays],A.[Address1],A.[Address2],A.[Address3],A.[City],A.[State],
A.[PostCode],A.[ContactEmailID],A.[ContactPhone],A.[PublicNote],A.[PrivateNote],A.[WorkingHoursSummary],
A.[IsShowOnWebSite],A.[IsActive],A.[IsDeleted],A.[CreateUserID],A.[CreateDateTime],A.[UpdateUserID],A.[UpdatedDateTime]

FROM OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=U:\xml AlamoFBL locations sql upload.xls;HDR=YES', 'select * from Sheet') AS A;
-------------------------------------------------------------------------------

But poped up with error msg

Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-03 : 03:57:25
quote:
Originally posted by anuraag205

Yes VIsakh,

------------------
INSERT INTO [tblsupplierlocation]
SELECT [pk_SupplierLocationID],[423],[fk_CountryID],[fk_LocationID],[IsAirport],[IsMeetnGreet],
[IsDeskInTermainl],[Description],[IsShutOnHolidays],[Address1],[Address2],[Address3],[City],[State],
[PostCode],[ContactEmailID],[ContactPhone],[PublicNote],[PrivateNote],[WorkingHoursSummary],
[IsShowOnWebSite],[IsActive],[IsDeleted],[CreateUserID],[CreateDateTime],[UpdateUserID],[UpdatedDateTime]
FROM [tblsupplierlocation]
WHERE [fk_SupplierID]in (414)

----------------------------------------------------

and the error shows "Msg 207, Level 16, State 1, Line 2
Invalid column name '423'.
Msg 8101, Level 16, State 1, Line 2
An explicit value for the identity column in table 'tblsupplierlocation' can only be specified when a column list is used and IDENTITY_INSERT is ON.
---------------------------------------------------------

So could not guess where the error lies...
Sorry yaar

Thanks


thats because one of the involved columns (i think pk) is an identity column, so you cant directly insert a value for it ( I would reckon you shouldnt if its a pk)

in that case, just ignore that column altogether and do like


INSERT INTO [tblsupplierlocation]
(all columns except primary key column listed here)
SELECT 423,[fk_CountryID],[fk_LocationID],[IsAirport],[IsMeetnGreet],
[IsDeskInTermainl],[Description],[IsShutOnHolidays],[Address1],[Address2],[Address3],[City],[State],
[PostCode],[ContactEmailID],[ContactPhone],[PublicNote],[PrivateNote],[WorkingHoursSummary],
[IsShowOnWebSite],[IsActive],[IsDeleted],[CreateUserID],[CreateDateTime],[UpdateUserID],[UpdatedDateTime]
FROM [tblsupplierlocation]
WHERE [fk_SupplierID]in (414)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-03 : 04:00:15
quote:
Originally posted by anuraag205

I also tried to update from excel

INSERT INTO [tblsupplierlocation]
([pk_SupplierLocationID],[fk_SupplierID],[fk_CountryID],[fk_LocationID],[IsAirport],[IsMeetnGreet],
[IsDeskInTermainl],[Description],[IsShutOnHolidays],[Address1],[Address2],[Address3],[City],[State],
[PostCode],[ContactEmailID],[ContactPhone],[PublicNote],[PrivateNote],[WorkingHoursSummary],
[IsShowOnWebSite],[IsActive],[IsDeleted],[CreateUserID],[CreateDateTime],[UpdateUserID],[UpdatedDateTime])

SELECT A.[pk_SupplierLocationID],
A.[fk_SupplierID],
A.[fk_CountryID],A.[fk_LocationID],A.[IsAirport],A.[IsMeetnGreet],
A.[IsDeskInTermainl],A.[Description],A.[IsShutOnHolidays],A.[Address1],A.[Address2],A.[Address3],A.[City],A.[State],
A.[PostCode],A.[ContactEmailID],A.[ContactPhone],A.[PublicNote],A.[PrivateNote],A.[WorkingHoursSummary],
A.[IsShowOnWebSite],A.[IsActive],A.[IsDeleted],A.[CreateUserID],A.[CreateDateTime],A.[UpdateUserID],A.[UpdatedDateTime]

FROM OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=U:\xml AlamoFBL locations sql upload.xls;HDR=YES', 'select * from Sheet') AS A;
-------------------------------------------------------------------------------

But poped up with error msg

Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

Thanks


As error suggests you've to enable the option Ad Hoc Distributed Queries if you want to use OPENROWSET etc. Its disabled by default and can be enbled as


EXEC sp_configure 'show advanced option', '1'
GO
RECONFIGURE WITH OVERRIDE
GO

EXEC sp_configure 'Ad Hoc Distributed Queries', '1'
GO
RECONFIGURE WITH OVERRIDE
GO



Hopefully you do have the necessary access rights to execute sp_configure

My recommendation would be to use previous method without trying to fetch from excel
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2013-06-03 : 04:30:49
GREAT NEWS VISAKH...THIS WORKED...THANKS FOR YOUR SUGGESTIONS

INSERT INTO tblsupplierlocation
(fk_SupplierID,fk_CountryID,fk_LocationID,IsAirport,IsMeetnGreet,
IsDeskInTermainl,Description,isShutOnHolidays,Address1,Address2,Address3,City,State,
PostCode,ContactEmailID,ContactPhone,PublicNote,PrivateNote,WorkingHoursSummary,
IsShowOnWebSite,IsActive,IsDeleted,createUserID,CreateDateTime,updateUserID,UpdatedDateTime)
SELECT 423,[fk_CountryID],[fk_LocationID],[IsAirport],[IsMeetnGreet],
[IsDeskInTermainl],[Description],[IsShutOnHolidays],[Address1],[Address2],[Address3],[City],[State],
[PostCode],[ContactEmailID],[ContactPhone],[PublicNote],[PrivateNote],[WorkingHoursSummary],
[IsShowOnWebSite],[IsActive],[IsDeleted],[CreateUserID],[CreateDateTime],[UpdateUserID],[UpdatedDateTime]
FROM [tblsupplierlocation]
WHERE [fk_SupplierID]in (414)


THIS WORKED...THANKS FOR YOUR SUGGESTIONS


Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-03 : 04:45:07
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -