Cant read count(*) value in temporary table - sql

I want to fill a temporary table with values from another table and a count value, but it always throws the error:
Column name or number of supplied values does not match table
definition
My code looks like this:
CREATE TABLE #TempTable
([ObjectId] uniqueidentifier, [ListName] nvarchar(255), [HowMany] int)
INSERT INTO #TempTable
SELECT [ObjectId]
,[ListName]
,(SELECT COUNT(*) FROM [ATable] as a WHERE [ObjectId] = a.FK_ObjectId ) AS [HowMany]
FROM [AnotherTable]
It works fine for the other columns but not for the [HowMany] column.
I already tried to cast the SELECT count(*) to an integer but that also did not work.
What am I doing wrong?

I'm checking your request, I think that query below will help you, take a look please, I didn't use a temp table but is just the update the table names to be able to use a temp table.
As you didn't show us the structure of your table, I also made an example to make it simpler to understand, I think :-).
DECLARE #Category as table (
id int,
category nvarchar(50)
);
DECLARE #Product as table (
id int,
idCategory int,
productName nvarchar(50)
);
INSERT INTO #Category
VALUES
(1, 'Category 1'),
(2, 'Category 2'),
(3, 'Category 3')
INSERT INTO #Product
VALUES
(1,1, 'Product 1'),
(2,1, 'Product 2'),
(3,1, 'Product 3'),
(4,2, 'Product 4'),
(5,2, 'Product 5'),
(6,2, 'Product 6'),
(7,2, 'Product 7'),
(8,2, 'Product 8'),
(9,3, 'Product 9'),
(10,3, 'Product 10'),
(11,3, 'Product 11'),
(12,3, 'Product 12')
--To know how many product is used by category
SELECT * FROM
(
SELECT
C.*,
(Count(P.id) over (partition by C.ID order by C.category)) as HowManyProductByCategory
FROM
#Category as C
INNER JOIN #Product as P ON C.id = P.idCategory
) as T
GROUP BY T.id, T.category, T.HowManyProductByCategory
The result
id category HowManyProductByCategory
1 Category 1 3
2 Category 2 5
3 Category 3 4
Best Regards

Related

SQL LEFT JOIN to many categories

Suppose the following easy scenario, where a product row gets connected to one primary category, subcategory, and sub-subcategory.
DECLARE #PRODUCTS TABLE (ID int, DESCRIPTION varchar(50), CAT varchar(30), SUBCAT varchar(30), SUBSUBCAT varchar(30));
INSERT #PRODUCTS (ID, DESCRIPTION, CAT, SUBCAT, SUBSUBCAT) VALUES
(1, 'NIKE MILLENIUM', '1', '10', '100'),
(2, 'NIKE CORTEZ', '1', '12', '104'),
(3, 'ADIDAS PANTS', '2', '27', '238'),
(4, 'PUMA REVOLUTION 5', '3', '35', '374'),
(5, 'SALOMON SHELTER CS', '4', '15', '135'),
(6, 'NIKE EBERNON LOW', '2', '14', '157');
DECLARE #CATS TABLE (ID int, DESCR varchar(100));
INSERT #CATS (ID, DESCR) VALUES
(1, 'MEN'),
(2, 'WOMEN'),
(3, 'UNISEX'),
(4, 'KIDS'),
(5, 'TEENS'),
(6, 'BACK TO SCHOOL');
DECLARE #SUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBCATS (ID, DESCR) VALUES
(10, 'FOOTWEAR'),
(12, 'OUTERWEAR'),
(14, 'SWIMWEAR'),
(15, 'HOODIES'),
(27, 'CLOTHING'),
(35, 'SPORTS');
DECLARE #SUBSUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBSUBCATS (ID, DESCR) VALUES
(100, 'RUNNING'),
(104, 'ZIP TOPS'),
(135, 'FLEECE'),
(157, 'BIKINIS'),
(238, 'PANTS'),
(374, 'JOGGERS');
SELECT prod.ID,
prod.DESCRIPTION,
CONCAT(cat1.DESCR, ' > ', cat2.DESCR, ' > ', cat3.DESCR) AS CATEGORIES
FROM #PRODUCTS AS prod
LEFT JOIN #CATS AS cat1 ON cat1.ID = prod.CAT
LEFT JOIN #SUBCATS AS cat2 ON cat2.ID = prod.SUBCAT
LEFT JOIN #SUBSUBCATS AS cat3 ON cat3.ID = prod.SUBSUBCAT;
Now suppose that the foreign keys on #PRODUCTS table aren't just indices to their respective tables. They are comma-separated indices to more than one categories, subcategories, and sub-subcategories like here.
DECLARE #PRODUCTS TABLE (ID int, DESCRIPTION varchar(50), CAT varchar(30), SUBCAT varchar(30), SUBSUBCAT varchar(30));
INSERT #PRODUCTS (ID, DESCRIPTION, CAT, SUBCAT, SUBSUBCAT) VALUES
(1, 'NIKE MILLENIUM', '1, 2', '10, 12', '100, 135'),
(2, 'NIKE CORTEZ', '1, 5', '12, 15', '104, 374'),
(3, 'ADIDAS PANTS', '2, 6', '27, 35', '238, 374');
DECLARE #CATS TABLE (ID int, DESCR varchar(100));
INSERT #CATS (ID, DESCR) VALUES
(1, 'MEN'),
(2, 'WOMEN'),
(3, 'UNISEX'),
(4, 'KIDS'),
(5, 'TEENS'),
(6, 'BACK TO SCHOOL');
DECLARE #SUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBCATS (ID, DESCR) VALUES
(10, 'FOOTWEAR'),
(12, 'OUTERWEAR'),
(14, 'SWIMWEAR'),
(15, 'HOODIES'),
(27, 'CLOTHING'),
(35, 'SPORTS');
DECLARE #SUBSUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBSUBCATS (ID, DESCR) VALUES
(100, 'RUNNING'),
(104, 'ZIP TOPS'),
(135, 'FLEECE'),
(157, 'BIKINIS'),
(238, 'PANTS'),
(374, 'JOGGERS');
SELECT prod.ID,
prod.DESCRIPTION
--CONCAT(cat1.DESCR, ' > ', cat2.DESCR, ' > ', cat3.DESCR) AS CATEGORIES
FROM #PRODUCTS AS prod
--LEFT JOIN #CATS AS cat1 ON cat1.ID = prod.CAT
--LEFT JOIN #SUBCATS AS cat2 ON cat2.ID = prod.SUBCAT
--LEFT JOIN #SUBSUBCATS AS cat3 ON cat3.ID = prod.SUBSUBCAT;
In this case I want to achieve the following:
Be able to retrieve the respective names of the cats, subcats, sub-subcats, ie. for cats '1, 2' be able to retrieve their names (I tried LEFT JOIN #CATS AS cat1 ON cat1.ID IN prod.CAT but it doesn't work)
Create triplets of the corresponding cats, subcats, sub-subcats, ie. for
cats '1, 2'
subcats '12, 17'
sub-subcats '239, 372'
(after retrieving the appropriate names) create pipe-separated category routes like name of cat 1 > name of subcat 12 > name of sub-subcat 239 | name of cat 2 > name of subcat 17 > name of sub-subcat 372
So, for a row like (1, 'NIKE MILLENIUM', '1, 2', '10, 12', '100, 135'),
I would like to get the following result
ID
DESCRIPTION
CATEGORIES
1
NIKE MILLENIUM
MEN > FOOTWEAR > RUNNING # WOMEN > OUTERWEAR > FLEECE (I had to use # as the delimiter of the two triplets because pipe messed with the table's columns)
In case the user stupidly stores more cat IDs than subcat IDs, or sub-subcat IDs, the query should just match the ones that have a corresponding position match, ie for
cats '1, 2'
subcats '12'
sub-subcats '239, 372'
it should just create one triplet, like name of 1 > name of 12 > name of 239
STRING_SPLIT() does not promise to return the values in a specific order, so it won't work in this case as ordinal position matters.
Use OPENJSON() split the string into separate rows to ensure the values are returned in the same order.
OPENJSON() also returns a key field, so you can join on the row number within each grouping. You'll want an INNER JOIN since your requirement is that all values in that "column" must exist.
Use STUFF() to assemble the various cat>subcat>subsubcat values.
DECLARE #PRODUCTS TABLE (ID int, DESCRIPTION varchar(50), CAT varchar(30), SUBCAT varchar(30), SUBSUBCAT varchar(30));
INSERT #PRODUCTS (ID, DESCRIPTION, CAT, SUBCAT, SUBSUBCAT) VALUES
(1, 'NIKE MILLENIUM', '1, 2', '10, 12', '100, 135'),
(2, 'NIKE CORTEZ', '1, 5', '12, 15', '104, 374'),
(3, 'ADIDAS PANTS', '2, 6, 1', '27, 35, 10', '238, 374, 100'),
(4, 'JOE THE PLUMBER JEANS', '1, 5', '27', '238, 374');
DECLARE #CATS TABLE (ID int, DESCR varchar(100));
INSERT #CATS (ID, DESCR) VALUES
(1, 'MEN'),
(2, 'WOMEN'),
(3, 'UNISEX'),
(4, 'KIDS'),
(5, 'TEENS'),
(6, 'BACK TO SCHOOL');
DECLARE #SUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBCATS (ID, DESCR) VALUES
(10, 'FOOTWEAR'),
(12, 'OUTERWEAR'),
(14, 'SWIMWEAR'),
(15, 'HOODIES'),
(27, 'CLOTHING'),
(35, 'SPORTS');
DECLARE #SUBSUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBSUBCATS (ID, DESCR) VALUES
(100, 'RUNNING'),
(104, 'ZIP TOPS'),
(135, 'FLEECE'),
(157, 'BIKINIS'),
(238, 'PANTS'),
(374, 'JOGGERS');
;
with prod as (
SELECT p.ID,
p.DESCRIPTION
--CONCAT(cat1.DESCR, ' > ', cat2.DESCR, ' > ', cat3.DESCR) AS CATEGORIES
, c.value as CatId
, c.[key] as CatKey
, sc.value as SubCatId
, sc.[key] as SubCatKey
, ssc.value as SubSubCatId
, ssc.[key] as SubSubCatKey
FROM #PRODUCTS p
cross apply OPENJSON(CONCAT('["', REPLACE(cat, ', ', '","'), '"]')) c
cross apply OPENJSON(CONCAT('["', REPLACE(subcat, ', ', '","'), '"]')) sc
cross apply OPENJSON(CONCAT('["', REPLACE(subsubcat, ', ', '","'), '"]')) ssc
where c.[key] = sc.[key]
and c.[key] = ssc.[key]
)
, a as (
select p.ID
, p.DESCRIPTION
, c.DESCR + ' > ' + sc.DESCR + ' > ' + ssc.DESCR as CATEGORIES
, p.CatKey
from prod p
inner join #CATS c on c.ID = p.CatId
inner join #SUBCATS sc on sc.ID = p.SubCatId
inner join #SUBSUBCATS ssc on ssc.ID = p.SubSubCatId
)
select DISTINCT ID
, DESCRIPTION
, replace(STUFF((SELECT distinct ' | ' + a2.CATEGORIES
from a a2
where a.ID = a2.ID
FOR XML PATH(''))
,1,2,''), '>', '>') CATEGORIES
from a
Totally separate answer because of the change to older technology. I think my original answer is still good for folks using current SQL Server versions, so I don't want to remove it.
I don't remember where I got the function. When I found it today it was named split_delimiter. I changed the name, added some comments, and incorporated the ability to have a delimiter that is more than one character long.
CREATE FUNCTION [dbo].[udf_split_string](#delimited_string VARCHAR(8000), #delimiter varchar(10))
RETURNS TABLE AS
RETURN
WITH cte10(num) AS ( -- 10 rows
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
)
, cte100(num) AS ( -- 100 rows
SELECT 1
FROM cte10 t1, cte10 t2
)
, cte10000(num) AS ( -- 10000 rows
SELECT 1
FROM cte100 t1, cte100 t2
)
, cte1(num) AS ( -- 1 row per character
SELECT TOP (ISNULL(DATALENGTH(#delimited_string), 0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM cte10000
)
, cte2(num) AS ( -- locations of strings
SELECT 1
UNION ALL
SELECT t.num + len(replace(#delimiter, ' ', '_'))
FROM cte1 t
WHERE SUBSTRING(#delimited_string, t.num, len(replace(#delimiter, ' ', '_'))) = #delimiter
)
, cte3(num, [len]) AS (
SELECT t.num
, ISNULL(NULLIF(CHARINDEX(#delimiter, #delimited_string, t.num), 0) - t.num, 8000)
FROM cte2 t
)
SELECT [Key] = ROW_NUMBER() OVER (ORDER BY t.num)
, [Value] = SUBSTRING(#delimited_string, t.num, t.[len])
FROM cte3 t;
GO
DECLARE #PRODUCTS TABLE (ID int, DESCRIPTION varchar(50), CAT varchar(30), SUBCAT varchar(30), SUBSUBCAT varchar(30));
INSERT #PRODUCTS (ID, DESCRIPTION, CAT, SUBCAT, SUBSUBCAT) VALUES
(1, 'NIKE MILLENIUM', '1, 2', '10, 12', '100, 135'),
(2, 'NIKE CORTEZ', '1, 5', '12, 15', '104, 374'),
(3, 'ADIDAS PANTS', '2, 6, 1', '27, 35, 10', '238, 374, 100'),
(4, 'JOE THE PLUMBER JEANS', '1, 5', '27', '238, 374');
DECLARE #CATS TABLE (ID int, DESCR varchar(100));
INSERT #CATS (ID, DESCR) VALUES
(1, 'MEN'),
(2, 'WOMEN'),
(3, 'UNISEX'),
(4, 'KIDS'),
(5, 'TEENS'),
(6, 'BACK TO SCHOOL');
DECLARE #SUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBCATS (ID, DESCR) VALUES
(10, 'FOOTWEAR'),
(12, 'OUTERWEAR'),
(14, 'SWIMWEAR'),
(15, 'HOODIES'),
(27, 'CLOTHING'),
(35, 'SPORTS');
DECLARE #SUBSUBCATS TABLE (ID int, DESCR varchar(100));
INSERT #SUBSUBCATS (ID, DESCR) VALUES
(100, 'RUNNING'),
(104, 'ZIP TOPS'),
(135, 'FLEECE'),
(157, 'BIKINIS'),
(238, 'PANTS'),
(374, 'JOGGERS');
;
with prod as (
SELECT p.ID,
p.DESCRIPTION
, c.value as CatId
, c.[key] as CatKey
, sc.value as SubCatId
, sc.[key] as SubCatKey
, ssc.value as SubSubCatId
, ssc.[key] as SubSubCatKey
FROM #PRODUCTS p
cross apply dbo.udf_split_string(cat, ', ') c
cross apply dbo.udf_split_string(subcat, ', ') sc
cross apply dbo.udf_split_string(subsubcat, ', ') ssc
where c.[key] = sc.[key]
and c.[key] = ssc.[key]
)
, a as (
select p.ID
, p.DESCRIPTION
, c.DESCR + ' > ' + sc.DESCR + ' > ' + ssc.DESCR as CATEGORIES
, p.CatKey
from prod p
inner join #CATS c on c.ID = p.CatId
inner join #SUBCATS sc on sc.ID = p.SubCatId
inner join #SUBSUBCATS ssc on ssc.ID = p.SubSubCatId
)
select DISTINCT ID
, DESCRIPTION
, replace(STUFF((SELECT distinct ' | ' + a2.CATEGORIES
from a a2
where a.ID = a2.ID
FOR XML PATH(''))
,1,2,''), '>', '>') CATEGORIES
from a
Well that should do work, i changed your character ">" for "-" just for see the data more simple.
the design of your tables is not perfect but the first try almost never is.
select mainp.ID, mainp.DESCRIPTION, stuff(ppaths.metapaths, len(ppaths.metapaths),1,'') metalinks
from #PRODUCTS mainp
cross apply(
select
(select
c.DESCR + '-' + sc.DESCR + '-' + sbc.DESCR + '|'
from #PRODUCTS p
cross apply (select row_number() over(order by Value) id, Value from split(p.CAT, ','))cat_ids
inner join #cats c on c.ID = cat_ids.Value
cross apply (select row_number() over(order by Value) id, Value from split(p.SUBCAT, ','))subcat_ids
inner join #SUBCATS sc on sc.ID = subcat_ids.Value
and subcat_ids.id = subcat_ids.id
cross apply (select row_number() over(order by Value) id, Value from split(p.SUBSUBCAT, ','))subsubcat_ids
inner join #SUBSUBCATS sbc on sbc.ID = subsubcat_ids.Value
and subsubcat_ids.id = subcat_ids.id
where p.id = mainp.ID
for xml path('')) metapaths
) ppaths
the link for split function
https://desarrolladores.me/2014/03/sql-server-funcion-split-para-dividir-un-string/

Multiple user entries for multiple client data recorded in single table. how to transform SQL

I'm trying to query my data which records multiple user entries for multiple client data in a single table distinguished by a unique group activity id.
Please see my table and intended output.
Looks to me as though you simply want all combinations of participants that are 'Users' with participants that are 'Clients' ...
-- Prep Work
IF OBJECT_ID('tempdb..#Table') IS NOT NULL DROP TABLE #Table
SELECT * INTO #Table FROM (VALUES
(1, 'Client A'),
(2, 'Client B'),
(3, 'Client C'),
(12, 'User A'),
(12, 'User B'),
(12, 'User C'),
(12, 'User D')
) S(activityId, activityParticipant)
Try this query against 'your' table ....
-- Actual Query
SELECT
U.activityParticipant,
C.activityParticipant
FROM
(SELECT activityParticipant FROM #Table WHERE activityParticipant LIKE 'User%') U
CROSS JOIN
(SELECT activityParticipant FROM #Table WHERE activityParticipant LIKE 'Client%') C
ORDER BY
U.activityParticipant,
C.activityParticipant

Sum of 2 columns if row exists in Table 2 - SQL

CREATE TABLE #Details
(
SName VARCHAR(20),
PName VARCHAR(20),
SoldCount INT,
Value INT
)
CREATE TABLE #DetailsException
(
ExSName VARCHAR(20),
ExPName VARCHAR(20)
)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 1', 'Product 1', 10,400)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 1', 'Product 3', 3,500)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 2', 'Product 1', 8,30)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 2', 'Product 2', 10,25)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 2', 'Product 2', 23,120)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 4', 'Product 1', 23,50)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 4', 'Product 3', 10,50)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 4', 'Product 5', 7,200)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 5', 'Product 1', 10,100)
INSERT INTO #Details(SName, PName, SoldCount,Value) VALUES ('Store 5', 'Product 1', 24,240)
INSERT INTO #DetailsException(ExSName, ExPName) VALUES ('Store 2', 'Product 2')
INSERT INTO #DetailsException(ExSName, ExPName) VALUES ('Store 4', 'Product 5')
SELECT SName, PName, **CASE WHEN EXISTS(SELECT 1 FROM #DetailsException WHERE ExSName = SName AND ExPName = PNAme ) THEN 0 ELSE SUM(SoldCount) END AS SoldCount**, SUM(Value) AS Value
FROM #Details
GROUP BY SNAME, PNAME
ORDER BY SNAME, PNAME
The result set is correct. Although I want to know if there any other optimized way of writing it in SQL. Since both the tables will grow extremely large in size.
Thanks.
I think I would be more inclined to write this as a LEFT JOIN:
SELECT d.SName, d.PName,
SUM(CASE WHEN de.ExSname IS NOT NULL THEN 0 ELSE SoldCount END) AS SoldCount,
SUM(Value) AS Value
FROM #Details d LEFT JOIN
#DetailsException de
ON de.ExSName = d.SName AND de.ExPName = d.PNAme
GROUP BY d.SNAME, d.PNAME
ORDER BY d.SNAME, d.PNAME;
However, your method is fine.
Considering the ExSName, ExPName is unique in
Here is another way using LEFT JOIN.
SELECT SName,
PName,
Sum(CASE
WHEN de.ExSName IS NULL THEN ( SoldCount )
ELSE 0
END) AS SoldCount,
Sum(Value) AS Value
FROM #Details d
LEFT JOIN (select distinct ExSName,ExPName from #DetailsException) de
ON de.ExSName = d.SName
AND de.ExPName = d.PNAme
GROUP BY SNAME,
PNAME
ORDER BY SNAME,
PNAME
Check the performance by running the queries. You can create Non-clustered index on SName,PNAme column to improve the performance
CREATE NONCLUSTERED INDEX IX_details_temp
ON #Details (SNAME, PNAME)
include (SoldCount, Value);
Added covering index (SoldCount, Value) to avoid Clustered/Heap lookup. Note, Index will be useful only if you are going to filter records from #details table. And for #DetailsException table creating the below index could be helpful
CREATE NONCLUSTERED INDEX IX_DetailsException_temp
ON #DetailsException (ExSName, ExPName)

SQL Expression Assistance

I am pulling data from using Telerik Standalone Reporting Application. I have a Table called "Product" one of the columns is called "ProductStatus". ProductStatus is an int value ranging from 1-12. Breakdown below:
"1"=Active
"2"=Retired
"3"=Processing
"5"=Archived
"6"=Active-Empty
"7"=Available
"8"=Resigned
"9"=Terminated
"10"=Legal Freeze
"11"=Admin Hold
"12"=Reserved
My question is: How can I write an Expression that will Look at "ProductStatus" and If = to 1 Then Return "Active" Or If = to 2 Then Return "Retired" etc.
You have 2 ways to do that
use CASE and code the translation table in the request
SELECT CASE WHEN 1 THEN 'Active' ... END FROM ...;
put the translation table in a database table and use a join :
CREATE TABLE product_status(id INTEGER, status VARCHAR(32));
INSERT INTO product_status(id, status) VALUES(1, 'Active');
...
SELECT ps.status, ... FROM product_status, ... WHERE product_status.id = ..., ... ;
You can create a table with all status texts like this:
CREATE TABLE statusText
([id] int, [text] varchar(12))
;
INSERT INTO statusText
([id], [text])
VALUES
(1, 'Active'),
(2, 'Retired'),
(3, 'Processing'),
(5, 'Archived'),
(6, 'Active-Empty'),
(7, 'Available'),
(8, 'Resigned'),
(9, 'Terminated'),
(10, 'Legal Freeze'),
(11, 'Admin Hold'),
(12, 'Reserved')
;
And then join Products table like this:
SELECT a.*, b.Text
FROM Products a INNER JOIN statusText b
ON a.ProductStatus=b.id

SQL Insert Multiple Rows

I want to insert multiple rows in a single table. How can I do this using single insert statement?
Wrap each row of values to be inserted in brackets/parenthesis (value1, value2, value3) and separate the brackets/parenthesis by comma for as many as you wish to insert into the table.
INSERT INTO example
VALUES
(100, 'Name 1', 'Value 1', 'Other 1'),
(101, 'Name 2', 'Value 2', 'Other 2'),
(102, 'Name 3', 'Value 3', 'Other 3'),
(103, 'Name 4', 'Value 4', 'Other 4');
You can use SQL Bulk Insert Statement
BULK INSERT TableName
FROM 'filePath'
WITH
(
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n'',
ROWS_PER_BATCH = 10000,
FIRSTROW = 2,
TABLOCK
)
for more reference check
https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=sql%20bulk%20insert
You Can Also Bulk Insert Your data from Code as well
for that Please check below Link:
http://www.codeproject.com/Articles/439843/Handling-BULK-Data-insert-from-CSV-to-SQL-Server
1--> {Simple Insertion when table column sequence is known}
Insert into Table1
values(1,2,...)
2--> {Simple insertion mention column}
Insert into Table1(col2,col4)
values(1,2)
3--> {bulk insertion when num of selected collumns of a table(#table2) are equal to Insertion table(Table1) }
Insert into Table1 {Column sequence}
Select * -- column sequence should be same.
from #table2
4--> {bulk insertion when you want to insert only into desired column of a table(table1)}
Insert into Table1 (Column1,Column2 ....Desired Column from Table1)
Select Column1,Column2..desired column from #table2
You can use UNION All clause to perform multiple insert in a table.
ex:
INSERT INTO dbo.MyTable (ID, Name)
SELECT 123, 'Timmy'
UNION ALL
SELECT 124, 'Jonny'
UNION ALL
SELECT 125, 'Sally'
Check here
For MSSQL, there are two ways:(Consider you have a 'users' table,below both examples are using this table for example)
1) In case, you need to insert different values in users table. Then you can write statement like:
INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe'),
(3, 'Linda', 'Mitchell'),
(4, 'Jillian', 'Carson'),
(5, 'Garrett', 'Vargas');
2) Another case, if you need to insert same value for all rows(for example, 10 rows you need to insert here). Then you can use below sample statement:
INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe')
GO 10
Hope this helps.
You can use the UNION ALL function
http://blog.sqlauthority.com/2007/06/08/sql-server-insert-multiple-records-using-one-insert-statement-use-of-union-all/
We will import the CSV file into the destination table in the simplest form. I placed my sample CSV file on the C: drive and now we will create a table which we will import data from the CSV file.
DROP TABLE IF EXISTS Sales
CREATE TABLE [dbo].[Sales](
[Region] [varchar](50) ,
[Country] [varchar](50) ,
[ItemType] [varchar](50) NULL,
[SalesChannel] [varchar](50) NULL,
[OrderPriority] [varchar](50) NULL,
[OrderDate] datetime,
[OrderID] bigint NULL,
[ShipDate] datetime,
[UnitsSold] float,
[UnitPrice] float,
[UnitCost] float,
[TotalRevenue] float,
[TotalCost] float,
[TotalProfit] float
)
The following BULK INSERT statement imports the CSV file to the Sales table.
BULK INSERT Sales
FROM 'C:\1500000 Sales Records.csv'
WITH (FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR='\n' );