Insert comma delimited ids into a table - sql

I need to insert records into a SQL Server table. The client send ids I need to insert into a specific column in the table:
(2,4,123,1357,1234,5657,753);
Using this function I'm splitting the comma delimited string, but not sure how to insert it to the table along with the other columns
I need to create something that will generate inserts such as:
insert into table_name (id,column_2,column_3) values (2, column_s_some_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (4, column_s_some_other_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (123, column_s_some_value, column_3_some_value);
ETC...
How can I achieve that?

the split function is a Table-Valued Function, which means it can be treated as a table, and you can do an INSERT..SELECT
insert into table_name (id,column_2,column_3)
SELECT s.item, column_s_some_value, column_3_some_value
FROM Split(#input_string, ',') s
{JOINS if needed to get other column values}

If you do want to split strings, the most efficient way to get that done is through the use of the tally table as outlined here by Jeff Moden. I'd strongly suggest you use this method in just about any version of SQL Server.

Related

Selecting rows where cells contain multiple values

Using Oracle SQL Developer 1.5.5.
I have a data table and I'm trying to select rows from the table (TABLENAME) where a column (COLUMN5) in the table contains multiple values. For example using the following command:
select * from TABLENAME where COLUMN5=''101','221','429''
I'm doing this because I want to select all rows in the table where the cell values in column 5 are '101','221',229'
An added complication is that in the table each of the values is within single quotation marks, separated by a comma. When run the above command I get an error message which says " SQL command not ended properly" I think this is something to do with the single quote marks and the multiple values because when i run the following command to select rows when column 5 is '443', I get a result:
select * from TABLENAME where COLUMN5='''443'''
Note that these values of '443' have single quotation marks around them in the table.
My question is what command will allow me to select rows with the multiple values in column 5? Help!
Let me know if you require any clarification
I think you can use FIND_IN_SET like below:
select * from accounts where FIND_IN_SET('101','101,102')
FIND_IN_SET ignore the single quotes
you just need to escape the quotes
create table so_col5 (a integer, col5 varchar2(25));
insert into so_col5 values (1, 'abcd');
insert into so_col5 values (2, 'abcd');
insert into so_col5 values (3, 'abcd');
Insert into SO_COL5 (A,COL5) values (4,'''101'',''221'',''229''');
commit;
select * from so_col5 where col5 = '''101'',''221'',''229''';
It took me a few tries, testing selecting the string from dual, but then I remembered I could just cheat - and export the result from the GRID as an INSERT Script and copy/paste the quoted string for that value and use that in my SELECT.
You're using v1.5, which is ANCIENT. Highly suggest you upgrade for an optimal experience.
You can also use the Q function (docs) which you can pass a string and Oracle handles the quote escaping for you. Here's a scenario you can play with on LiveSQL.

inserting data via SQL script

I have a CSV file which consists of one column. I want to insert (not import) all the elements in the columns into the database table. I know that if I wanted to insert few elements, then I can use the below statement to insert individually.
INSERT INTO table(column_name )
VALUES (element1);
But is there a method that I can insert all the elements at once?
You can just comma separate the values like below. I sometimes use Excel to do the formatting if you have a lot of values.
INSERT INTO table
(column_name)
VALUES
(element1), (element2)

SQL How to insert null value

I want to insert data into one table from another table. In some of the columns I don't have data, so I want to set column to null. I don't know how I should do this?
This is the SQL:
INSERT INTO _21Appoint(
PCUCODE,PID,SEQ,
DATE_SERV,APDATE,
APTYPE,APDIAG,D_UPDATE,CID
) SELECT (
NULL,NULL,NULL,
treatment_date,appointment_date,
typeap_id,appointment_id,NULL,patient_id
) FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
Your insert is essentially correct. Just don't put the column list in parentheses:
INSERT INTO _21Appoint
(PCUCODE,PID,SEQ,DATE_SERV,APDATE,APTYPE,APDIAG,D_UPDATE,CID)
SELECT NULL,NULL,NULL,treatment_date,appointment_date,typeap_id,appointment_id,NULL,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
In Postgres (unlike other DBMS) putting a column list in parentheses makes the result a single "record", rather then individual columns. And therefore the select only returns a single column, not multiples and thus it doesn't match the column list for the insert
another option is to simply leave out the columns completely:
INSERT INTO _21Appoint
(DATE_SERV,APDATE,APTYPE,APDIAG,CID)
SELECT treatment_date,appointment_date,typeap_id,appointment_id,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;

Insert multiple rows into single column

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.

SQL: Insert multiple sets of values in one statement?

Is it possible to insert multiple sets of values to a SQLite table in one statement?
I was trying:
INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo');
with the different ()s representing different insert sets, but I get an error.
Are there only three columns in your table? If not, you could try defining the column names you are setting like so:
INSERT INTO the_table
(column1 ,column2 ,column3)
VALUES (1 ,2 ,'hi' )
,(2 ,0 ,'foo' )
This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.
You can do
INSERT INTO the_table
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';
I was found that syntax in MSDN but after trying I can't do that too, than I note that in the bottom of the page was written that there is an error in the page :) where is link http://msdn.microsoft.com/en-us/library/ms174335.aspx see the bottom How to insert multiple rows