I have a user table. I want to insert data into my user table.
I have a statement:
SELECT columna, columnb,
INTO my_table
FROM my_other_table
WHERE (... conditions ...)
I get the following error:
SQL Server Error on (myserver) Error:2714 at Line:1 Message:There is already an object named 'my_table' in the database.
Yes, thanks Sybase. I know this. I know the table exists. I want to insert data into it.
Why is Sybase not playing nicely? :(
(Sybase isn't my forte, Oracle is. This may just be an understanding issue, or lack there of. This would never happen in Oracle...)
SELECT ... INTO is for creating new tables.
Use INSERT ... SELECT for existing tables. eg:
INSERT INTO my_table
SELECT columna, columnb,
FROM my_other_table
WHERE (... conditions ...)
Have you tried it this way around?
Insert INTO my_table
SELECT columna, columnb,
FROM my_other_table
WHERE (... conditions ...)
It appears that it is trying to implicitly create a new table for you called my_table.
Not sure of SYBASE but in DB2 this works for me
INSERT INTO my_table
(
columna,
columnb
)
SELECT
columna,
columnb
FROM
my_other_table
WHERE
(... conditions...)
I think its safer to specify the columns in the insert statement as well rather than assume they'll be in the same order as the select.
Use 'existing' keyword after 'into' to insert in existing table.
SELECT [COLUMN LIST] INTO EXISTING [TABLE NAME]
Related
insert into ASSET_MAIN_CATEGORIES values(select max(sno) from ASSET_MAIN_CATEGORIES,
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);
I wanted to insert max of SNO as column value. But It is showing "Missing Expression Error"
How can I achieve this.
Any Help would be appreciated.
There are at least ways of doing it:
INSERT INTO ... VALUES with embedded select in parentheses so that database will evaluate it:
insert into ASSET_MAIN_CATEGORIES values(
(select max(sno) from ASSET_MAIN_CATEGORIES),
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
);
INSERT INTO ... SELECT since all other data is static:
insert into ASSET_MAIN_CATEGORIES
select
max(sno),
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
from ASSET_MAIN_CATEGORIES
;
Note that if you do not specify which columns you are populating in ASSET_MAIN_CATEGORIES database assumes that you're feeding values for all of them in the order that they were created in this table.
You need to surround select max with brackets, because it's a SQL statement:
insert into ASSET_MAIN_CATEGORIES values((select max(sno) from ASSET_MAIN_CATEGORIES), 'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);
If you have an error, prefer to add column names in insert statement
this will work :-
i have checked with my tables its working ,sample code to understand:
insert into b(col1,col2) select 7,(select max(col2) from b)
from dual;
what code you require:-
insert into ASSET_MAIN_CATEGORIES(col1,col2,....,col10)
select (select max(col2) from ASSET_MAIN_CATEGORIES), 'PROD','AC
HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1)
from dual;
I have a view as below in sql server:
use database2
Go
CREATE VIEW view1
AS
WITH date_cte(datecol)
AS (select getdate())
Select Col1,
Col2,
,....
,[Select datecol from date_cte]
FROM database1.schema1.TABLE
on top a table in different database.
The record count of table as well as view using statement
Select count(1) from database1.schema1.TABLE -- 15487212
Select count(1) from database2.schema2.view1 -- 13324921
Does this problem have any solution?
First of all... you definitely should improve your code:
USE database2
Go
CREATE VIEW schema2.view1
AS
SELECT Col1,
Col2,
,....
,getdate()
FROM database1.schema1.TABLE
Why do you use a CTE just for the date? Another thing, which may cause your error. You create a view without defining your schema. Maybe your view is created but in a different schema? I've added schema2 to your CREATE due to the fact your querying schema2 at the end.
By the way. Your select can be improved too:
Select count(*) from database1.schema1.TABLE -- ??
Select count(*) from database2.schema2.view1 -- ???
Actually the issue here was that data was loading into source table while I was running query to count records.
Therefore the count difference was prevailing.
Thanks for your input
The same question has been asked for MySQL here, anyway that syntax doesn't work in SQL Server.
I paste the sample from that question (by simplifying it) here because it explains what I need very well.
DECLARE #t1 integer
SET #t1=0;
-- MyTable is a simple Table with 2 fields "MyID" and "MyValue"
insert into MyTable
SELECT #t1 := #t1+1, --this "should" work in MySQL
MyAnotherValue
FROM AnotherTable
Is there a way to achieve the same in SQL Server (without using a cursor)?
Note: this is a to be run once query, it is a maintenance query, so race conditions and locks are not an issue.
This will work, even if you run it more than once:
insert into MyTable(MyId, MyValue)
select (select isnull(max(MyId), 0) from MyTable) + -- avoid duplicated keys, if you repeat this insert again
row_number() over(order by MyAnotherValue),
MyAnotherValue
from AnotherTable
The isnull() function covers the case when MyTable is empty
You can achieve this by using Row_Number()
Insert Into dbo.AnotherTable (Col1, Col2)
Select Row_Number() Over(Order By SomeColumn),
SomeColumn
From dbo.YourTable
I am trying to insert a set of records into a table using
Insert into tbl1 Select * from tbl
One of the records failed due to check constrain in tbl1. But i want to insert the other records which have passed the check constraint and others i want to catch them as exception. Could someone please help.
In that case, you need to be more selective about your SELECT - exclude those rows that are trouble from your selection:
INSERT INTO dbo.tbl1(Col1, Col2, ...., ColN)
SELECT Col1, Col2, ....., ColN
FROM dbo.tbl
WHERE (some condition here to exclude those rows that don't match the `CHECK` constraint.....)
How do I transfer some rows from one table to another in the easiest possible manner? This is a one time thing. I was hoping I could use VS2010 copy and paste function in Server Explorer, but it doesn't allow me to paste in rows.
As the table schema is the same then you can use copy and paste using Sql Server Management Studio rather than VS2010
You could also use a T-SQL statement using SSMS
Insert Into dbo.TableB (ColumnA, ColumnB, ColumnC ...)
Select ColumnA, ColumnB, ColumnC ...
From dbo.TableA
select * into <destination table> from <source table>
Example:
select * into employee_backup from employee
You can add WHERE clauses and specify columns as needed.
As per http://cavemansblog.wordpress.com/2009/06/09/sql-server-how-to-copy-a-table/