how to insert single column using sql insert query
insert into table value(test's);
By specifying the column name
Let's assume the table has 3 columns c1, c2, and c2
INSERT INTO myTable (c1)
VALUES ('c1v1'),
('c1v2')
OR
INSERT INTO myTable (c1)
SELECT 'c1v1' UNION
SELECT 'c1v2'
Assuming you are wanting to INSERT a value with an apostrophe, you need to use an escape:
INSERT INTO table
VALUES
('test''s')
are you asking about inserting a column? if yes then
ALTER TABLE table_name
ADD column_name datatype;
else read this article about inserting into columns, it will help. it is very detailed
Related
I'm creating a C# Winforms application for recipe management in an industrial environment.
I created a SQL Server table with 130 columns. The table contains a column called CheckData (of datatype Timestamp), which I use to detect changes made to a row.
If I insert a new row to that table all works fine. The code I use is:
INSERT INTO tablename (Column1, column2, column3, column4)
VALUES (value1, value2, value3, value4)
I just assign values to major columns, the others get their default value. I do not assign a value to the timestamp field since it's written by the system.
Additionally, I want to copy a row from this table to the same table (duplicate a data record).
I copy the source row to a temporary table, drop the ID (primary key) and the timestamp fields in that temporary table and try to insert that only row in the temporary table into the table. This fails.
Here's the code:
SELECT *
INTO #temptable
FROM tablename
WHERE Recipe_No = 8;
ALTER TABLE #temptable DROP COLUMN ID, CHECKDATA;
ALTER TABLE #temptable REBUILD;
UPDATE #temptable
SET Recipe_No = 9, Recipe_Name = 'Test'
WHERE Recipe_No = 8;
INSERT INTO tablename
SELECT * FROM #temptable;
I don't understand where the difference is between inserting a new row thru INSERT INTO xxx (yyy) VALUES (zzz) and INSERT INTO xxx SELECT * FROM yyy. In both cases I don't try to write the timestamp value in the new row.
Does anybody have an idea what I'm missing here?
I don't understand where the difference is between inserting a new row thru INSERT INTO xxx (yyy) VALUES (zzz) and INSERT INTO xxx SELECT * FROM yyy.
With this,
INSERT INTO xxx SELECT * FROM yyy.
you are failing to specify the column mappings from the SELECT to the target table. You should always use
INSERT INTO xxx (Column1, Column2, . . .)
SELECT (Column1, Column2, . . .)
FROM yyy
Here's a simplified example of what you're attempting:
drop table if exists t
create table t(id int, a int)
insert into t(id,a) values (1,1)
select * into #t from t where id = 1
alter table #t drop column id
insert into t select * from #t
and it will fail with
Msg 213, Level 16, State 1, Line 12
Column name or number of supplied values does not match table definition.
because the temp table doesn't even have the same number of columns. And even if it did, you wouldn't know for sure that the column mappings were correct.
It is failing because essentially your command
INSERT INTO tablename SELECT * FROM #temptable;";
Is telling SQL - "Insert everything into this table from this temp table."
While you can work around this, I would say why don't you just try inserting into only the columns made available in your current table with only the values you would like to include. Instead of needing to drop the columns/values, you just don't import it to begin with.
An alternative - if you can write to a helper table, it may be beneficial to INSERT INTO that helper table, as opposed to a temp table, the values you have. Then transform that helper table, and THEN you can do INSERT INTO final_table SELECT * FROM helper. This should give you the results you're looking for.
I hope this is helpful, and I hope it explains why your current command is failing.
I'm new to SQL, (using SQL 2008 R2) and I am having trouble inserting multiple rows into a single column.
I have a table named Data and this is what I am trying
INSERT INTO Data ( Col1 ) VALUES
('Hello', 'World')
That code was taken from this question, but it, like many other examples I have found on the web uses 2 columns, I just want to use 1. What am I doing wrong?
Thanks
To insert into only one column, use only one piece of data:
INSERT INTO Data ( Col1 ) VALUES
('Hello World');
Alternatively, to insert multiple records, separate the inserts:
INSERT INTO Data ( Col1 ) VALUES
('Hello'),
('World');
to insert values for a particular column with other columns remain same:-
INSERT INTO `table_name`(col1,col2,col3)
VALUES (1,'val1',0),(1,'val2',0),(1,'val3',0)
I believe this should work for inserting multiple rows:
INSERT INTO Data ( Col1 ) VALUES
('Hello'), ('World'),...
Another way to do this is with union:
INSERT INTO Data ( Col1 )
select 'hello'
union
select 'world'
If your DBMS supports the notation, you need a separate set of parentheses for each row:
INSERT INTO Data(Col1) VALUES ('Hello'), ('World');
The cross-referenced question shows examples for inserting into two columns.
Alternatively, every SQL DBMS supports the notation using separate statements, one for each row to be inserted:
INSERT INTO Data (Col1) VALUES ('Hello');
INSERT INTO Data (Col1) VALUES ('World');
INSERT INTO Data ( Col1 ) VALUES ('Hello'), ('World')
In that code you are inserting two column value.
You can try this
INSERT INTO Data ( Col1 ) VALUES ('Hello'),
INSERT INTO Data ( Col1 ) VALUES ('World')
Kindly ensure, the other columns are not constrained to accept Not null values, hence while creating columns in table just ignore "Not Null" syntax. eg
Create Table Table_Name(
col1 DataType,
col2 DataType);
You can then insert multiple row values in any of the columns you want to.
For instance:
Insert Into TableName(columnname)
values
(x),
(y),
(z);
and so on…
Hope this helps.
INSERT INTO hr.employees (location_id) VALUE (1000) WHERE first_name LIKE '%D%';
let me know if there is any problem in this statement.
I am trying to come up with a DB2 SQL statement that does the following, but I am not sure
if this is allowed.
I know that it's possible to insert into tableA ( ... ) Values (?,?,?,...)
and then assign values to those parameters ?.
Is it possible to pre-defined the value of one of the parameter?
For example, one of the column that I am trying to insert is the ID column and I would like to make it something like select max(id) + 1 from tableA.
This is what I am trying to get to - is this syntax possible in db2?
insert into tableA (ID, Text1, Text2) VALUES (select max(id)+1 from tableA, ?, ?)
Anyways - any help would be appreciated!
thanks!!
this should works :
insert into tableA values((select max(id)+1 from tableA),'text1','text')
It sounds like you are wanting a primary key as an index on your table.
db2 alter table tableA add primary key (id)
This will create a column in your table which will auto-increment when you add a new record.
Source ( http://www.ibm.com/developerworks/data/library/techarticle/dm-0401melnyk/ )
You can also try using the OVERRIDING USER VALUE parameter:
INSERT INTO TableA
OVERRIDING USER VALUE
SELECT 0,Text1, Text2
From TableB
greetings all
I want to insert 70 records in a table
the values are value1,value2,...value70
and I was wondering if it's possible to accomplish this through a SQL query
I am using postgresql db.
thanks in advance.
Where do the values come from? Are they arbtrary values? Just use a multiple insert statement, like:
INSERT INTO t (colname) VALUES ('value1'), ('value2'), ..., ('value70');
Are they values that can be generated from the integers 1 to 70? If so, use the set returning function generate_series:
INSERT INTO t (colname) SELECT 'value'||i FROM generate_series(1,70) AS s(i);
Are they just in some other table? Just reference it normally:
INSERT INTO t (colname) SELECT val FROM othert;
Can I do this in SQL 2005?
SELECT 'C'+inserted.exhid AS ExhId,inserted.exhname AS ExhName,inserted.exhid AS RefID INTO mytable FROM inserted
WHERE inserted.altname IS NOT NULL
It won't work if the table exists, but will create the table if it is non-existent. How do I get it to insert into an existing table?
like this
INSERT INTO mytable
SELECT 'C'+inserted.exhid AS ExhId,inserted.exhname AS ExhName,
inserted.exhid AS RefID FROM inserted
WHERE inserted.altname IS NOT NULL
you also don't need the aliases in this case
SQLMenace's answer is correct. But to add to it, I would suggest that it is a good practice to explicitly list your columns that you are inserting into in the event the table structure/column order ever changes, that way your proc stands a better change of working consistently.
INSERT INTO mytable (
ExhId,
ExhName,
RefID)
SELECT 'C'+inserted.exhid,
inserted.exhname,
inserted.exhid
FROM inserted
WHERE inserted.altname IS NOT NULL
To insert into an existing table, use INSERT INTO instead of `SELECT INTO