inserting multiple values into a single cell - sql

i am trying to insert multiple values into a single cell column in my db. i am using a sql statment like this:
insert into table_name (mycol) values (1A,2B,3G,4,5,6F)
this table has only one field called "mycol"
the above query doesn't run and gives me an error. however, if i run it single entry like this:
insert into table_name (mycol) values (1A)
it works!
is there a way to feed sql with multiple values?

You have to break up the values portion like so (if you're using SQL Server)
insert into table_name (mycol) values (1A),(2B),(3G),(4),(5),(6F)

Based on this article, you can do something like this:
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO

I highly recommend against storing multiple values in a single field. You're better off, normally, with having seperate fields or multiple rows...
If student X is on Courses A, B, C;
INSERT INTO mapping_table (student_id, course_id)
SELECT 123, 1
UNION ALL SELECT 123, 2
UNION ALL SELECT 123, 3
If student X has 3 lines in his address;
INSERT INTO student_address (student_id, line1, line2, line3)
SELECT 123, '10 The Street', 'The Town', 'The City'

You need to pivot the selected values or do something like this:
INSERT dbo.Table_1(mycol)
SELECT * FROM
(
SELECT '1a' AS mycol UNION
SELECT '2b' UNION
SELECT '3g' UNION
SELECT '4' UNION
SELECT '5'
) T1

Related

How to add scope_identity() to UNION ALL syntax in SQL Server

I have a query for multiple inserts using UNION ALL.
How do I use SELECT scope_identity(); for each row of data inserted?
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First', 1
UNION ALL
SELECT 'Second', 2
UNION ALL
SELECT 'Third', 3
UNION ALL
SELECT 'Fourth', 4
UNION ALL
SELECT 'Fifth', 5
The short answer is no you can't. SCOPE_IDENTITY will give you the value of the last insert only. But you can use the OUTPUT clause from 2005 onwards. See https://msdn.microsoft.com/en-us/library/ms177564.aspx for information on how to use this clause.
You can use OUTPUT Clause to get Multiple Identity Column Value instead of scope_identity() while inserting Multiple Row.
Create Table tablename
(id int identity(1,5) Primary KEY, b int , c int)
DECLARE #tempIDs TABLE (id int)
INSERT tablename (b, c)
OUTPUT inserted.id INTO #tempIDs
SELECT 1, 1
UNION ALL
SELECT 2, 2
UNION ALL
SELECT 200, 3
UNION ALL
SELECT 3000, 4
UNION ALL
SELECT 400, 5
Select * from #tempIDs
Now your table variable #tempIDs have All Primary Key Data those are inserted using UNION ALL.

By using union select for inserting values into a temp table, how to insert values in the order I assigned?

I created a temp table and inserted some values into it by using union select.
CREATE TABLE tempdb..#tempName (ID int IDENTITY (1, 1) NOT NULL, Name varchar(20) NULL)
INSERT INTO #tempName(Name)
SELECT 'tommy'
UNION SELECT 'jimmy'
UNION SELECT 'adam'
UNION SELECT 'lucy'
Problem: I want to know how to insert the value as the order I wrote. Currently it inserted in Ascending order instead.
How I want the value to be inserted:
-- ID -- Name
-- 1 -- tommy
-- 2 -- jimmy
-- 3 -- adam
-- 4 -- lucy
The Order it inserted into the temp table:
-- ID -- Name
-- 1 -- adam
-- 2 -- jimmy
-- 3 -- lucy
-- 4 -- tommy
The reason you are getting things in alphabetical order is because of the union. It does additional processing to remove duplicates. That is why union all is usually recommended.
Using union all will probably almost always do what you want:
INSERT INTO #tempName(Name)
SELECT 'tommy'
UNION ALL SELECT 'jimmy'
UNION ALL SELECT 'adam'
UNION ALL SELECT 'lucy';
This approach works in practice and seems to produce a consistent execution plan that will produce the records in order. But, this behavior is not guaranteed.
Multiple INSERTs will definitely work.
INSERT INTO #tempName(Name) SELECT 'tommy';
INSERT INTO #tempName(Name) SELECT 'jimmy'
INSERT INTO #tempName(Name) SELECT 'adam'
INSERT INTO #tempName(Name) SELECT 'lucy';
And the VALUES statement will work:
INSERT INTO #tempName(Name)
VALUES (('tommy'), ('jimmy'), ('adam'), ('lucy'));

bulky insert record to database

I need to insert approximatlly 10 records.I am thinking foreach loop,do u think is it the best way to do that.whenn searching what is the best method to do that,I find something like this,
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
what is the differences between the this and foreach loop.which one is the better.
thank you all
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('First',1),
('Second',2),
('Third',3),
('Fourth',4),
('Fifth',5)
GO
Both the foreach loop and the union all methods would be what is called "Row by Agonizing Row" approach
If you have MSSQL 2008 or later, you can use multi-valued insert:
INSERT INTO mytable (id, name)
VALUES (1, 'One'),
(2, 'Two'),
(3, 'Three')
Unfortunately, this syntax is not supported by MSSQL 2005 and earlier.
Just Create a single for inserting all the record..
Because foreach loop will hit the database 10 times and u will take more time and resources..
So better to make single query for all the 10..
INSERT INTO Table ( FirstCol, SecondCol) VALUES
( Value1, Value2 ), ( Value1, Value2 )...

Compare one column values for the table in sql

I have table with X number of columns. One of them is nvarchar(50). Values of this column are like this:
13-46187(IC)
13-46186(IC)
13-46189
13-46185
13-46184
I want to extract/find the highest number that the column value ends with (in this case 189). How do I accomplish that?
This is hardcoded stuff. but will give you some ideas..
create table #temp
(
textfield varchar(50)
)
insert into #temp
select '13-46187(IC)'
UNION
select '13-46186(IC)'
UNION
select '13-46189'
UNION
select '13-46185'
UNION
select '13-46184'
select Max(Convert(int,substring(SUBSTRING(textfield, 6, LEN(textfield)), 1, 3)))
from #temp

Insert multiple values using INSERT INTO (SQL Server 2005)

In SQL Server 2005, I'm trying to figure out why I'm not able to insert multiple fields into a table. The following query, which inserts one record, works fine:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test')
However, the following query, which specifies more than one value, fails:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test'),(1001,N'test2')
I get this message:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.
When I looked up the help for INSERT in SQL Sever Management Studio, one of their examples showed using the "Values" syntax that I used (with groups of values in parentheses and separated by commas). The help documentation I found in SQL Server Management Studio looks like it's for SQL Server 2008, so perhaps that's the reason that the insert doesn't work. Either way, I can't figure out why it won't work.
The syntax you are using is new to SQL Server 2008:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test'),(1001,N'test2')
For SQL Server 2005, you will have to use multiple INSERT statements:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test')
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1001,N'test2')
One other option is to use UNION ALL:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
SELECT 1000, N'test' UNION ALL
SELECT 1001, N'test2'
You can also use the following syntax:-
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
From here
In SQL Server 2008,2012,2014 you can insert multiple rows using a single SQL INSERT statement.
INSERT INTO TableName ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
Another way
INSERT INTO TableName (Column1, Column2 )
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2