SQL 'simple' query syntax error - help! - sql

My query returns a syntax error:
Invalid object name 'table.clientinfo'.
Here is my query:
INSERT table.clientinfo (name, addr, entry, affiliate )
SELECT name, addr, entry, affiliate FROM table.clientinfo WHERE product = 5
Is the error due to the insert function not finding 'clientinfo' as it does not exist.
Can anybody give me the correct syntax to create the table first before populating it from the select function?

You want CREATE TABLE
CREATE TABLE clientinfo (
name VARCHAR(100)
addr VARCHAR(100)
entry VARCHAR(100)
affiliate VARCHAR(100)
);
with specific types/sizes for your app. You might want to indicate foriegn and primary keys, constraints etc too

In SQL Server, if you want to select and insert into a new table, use this syntax:
SELECT name, addr, entry, affiliate
INTO (new table name)
FROM [table.clientinfo]
WHERE product = 5
You need to SELECT .... INTO and you need to make sure to use proper table names.
If you table name really has a dot in it (really really bad practice!), then you MUST put that table name in square brackets: FROM [table.clientinfo]
Also, when doing SELECT .. INTO ... you cannot select from an existing table and insert into the same existing table - you need to use a new table name for your destination table.

You should use
INSERT INTO [tablename] (field1, field2, ... , fieldx)
SELECT ...
Or if you want to create the other data directly:
SELECT field1, field2, ... , fieldx
INTO newTable
FROM oldtable
WHERE ....

Related

Inserting data into a table(mutliple columns) which has primary key from another data which has data except primary key

I have a table that has 3 columns ID(Primary Key), Name, City.
I need to import data from another table that has only Name and City.
I can write insert into table 1(Name, City) select Name, City from table2.
But then I need ID in table 1 which needs to be inserted using a sequence.
I tried this:
insert into table1(ID, Name,City) values(seq.nextval, select distinct name, city from table2). But I am receiving an error saying an insufficient number of values.
I am trying it in SQL Oracle. Can someone please help me with this?
You are mixing the insert ... values and insert ... select syntax.
You edited your question to include distinct, implying you have duplicate name/city pairs that you want to suppress; but neither version gets the error you reported. If you don't have duplicates then you can just do:
insert into table1(ID, Name,City)
select seq.nextval, name, city from table2;
If you do have duplicates then you can't just add the distinct keyword, but you can use a subquery:
insert into table1 (id, name, city)
select seq.nextval, name, city
from (
select distinct name, city
from table2
);
db<>fiddle
You could also set the ID via a trigger. If you we're on a recent version you could use an identity column instead - but you tagged the question with Oracle 11g, where those are not available.

How can I make this an insert statement

I am new to SQL, So I have a table called 'DealerShip' and I have a many different car ID's . I have a dealership called 'Hondo' and another one called 'Mitch' . I would like to insert about 80 records into 'Mitch' that 'Hondo' has . For instance with this Query
select * from DealerShips where name='Hondo' and CarType=63
That query above contains about 70 records, How can I create an insert statement that will insert all the returned records from that query above ? The insert will go into the same table above except that the name will be 'Mitch' . I am using MSSQL 2012
INSERT INTO SELECT
INSERT INTO yourtable (FIELDS...) ---- fields here should match the select fields
SELECT FIELDS... FROM
DealerShips where name = 'Hondo' and CarType=63
Just make sure you list the columns in the insert and the select in the same order.
INSERT INTO DealerShips (name, cartype, more columns)
SELECT
'Mitch'
, cartype
, more columns
from DealerShips where name='Hondo' and CarType=63

SQL Server : updating one column and inserting if not exist

I have a table TableKats that looks like this:
ID - int
Name - varchar
KatID - int
What I want to do is to update the column Name from another table, and if there is a name in the other table that doesn't exist in TableKats, it should insert it and give KatID a 0
Does anybody know a way to do that? Thanks
you can do it using MERGE, as your other table schema is not known assuming Name as the column in other table too
MERGE TableKats T
USING ( SELECT * from TableB) AS S
ON T.Name = S.Name
WHEN NOT MATCHED THEN
INSERT ( Name, KatID)
VALUES ( S.Name, 0)
WHEN MATCHED THEN
UDPATE -- Not clear what needs to be updated.

How to use if not exists in sql

I am trying to use if exists statement but having some issues. I am checking if LOCATION and NAME exist in my NEWTABLE, if they don't exist then i want to insert them into my NewTable. So basically i am trying to find Location and Names where records exist in the OldTable but not in the newTable. thanks
Here is my sql
INSERT INTO NewTable(Location, Name)
SELECT Location, Name
FROM OldTable
WHERE Location and Name NOT IN (select Location, Name from NewTable)
You would use not exists for this:
INSERT INTO NewTable(Location, Name)
SELECT Location, Name
FROM OldTable ot
WHERE NOT EXISTS (select 1
from NewTable nt
where nt.LOCATION = ot.LOCATION and nt.NAME = ot.NAME
);
You can also enforce this constraint by building a unique index:
create unique index NewTable_Location_Name on NewTable(Location, Name);
Note that this would cause an error when duplicates are encountered (without the additional check).

Insert some variable columns with constants

What I'm looking to do with my code is insert some variable number of rows into test_table where 'policy' in source_table matches 'bond_ser' in policy_custsgt and 'SNL_ID' in source_table matches 'inst_key' in raw_table.
I want it to insert serial_number and ID along with some other constants. I can get it to insert serial_number and ID just fine, but how do I get it to add some constants in other fields of the table every time it inserts the variables from the other tables?
This is the code I currently have, if I remove the "'122812', '999999', 'myname'" from the first line it works fine but will only populate the serial_number and ID columns of my table with each insert.
INSERT INTO test_table(serial_number, ID, '122812', '999999', 'myname')
SELECT policy, SNL_ID
FROM source_table
WHERE (policy IN (SELECT bond_ser from policy_custsgt)) AND
(SNL_ID in (select inst_key from raw_table))
Thanks!
Of course, I over looked the simple solution. All I needed to do was move those constants I wanted down from the INSERT statement to the SELECT statment, and add the column names in the INSERT so:
INSERT INTO test_table(serial_number, ID, starting_date, ending_date, user_id)
SELECT policy, SNL_ID, '122812', '999999', 'myname'
FROM source_table
WHERE (policy IN (SELECT bond_ser from policy_custsgt)) AND
(SNL_ID in (select inst_key from raw_table))