Trying to add 10 values to sql database [closed] - sql

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
This is easy enough task under sql management studio. I clicked on edit top 200 rows. And entered first set of values as:
Everything should be same except Index and rollnumber
Index class rollnumber age
1 A 1 10
2 A 2 10
3 A 3 10
...
10 A 10 10
Problem i am having is:
I copied first row and pasted in second row and when i try to change it in sql editor i am unable to.
Can anyone please tell me what is the best way to do this through sql editor?
Or if there is easier way to do SQL insert in this case?

You can make sql queries and execute it in editor.
insert into table values( 1, A, 1, 10);
insert into table values( 2, A, 2, 10);
insert into table values( 3, A, 3, 10);
insert into table values( 4, A, 4, 10);
insert into table values( 5, A, 5, 10);
insert into table values( 6, A, 6, 10);
insert into table values( 7, A, 7, 10);
insert into table values( 8, A, 8, 10);
insert into table values( 9, A, 9, 10);
insert into table values( 10, A, 10, 10);

Try to copy it without the Index field if you have it as primary key.

I assume the Index is an IDENTITY column. The database will assign the value
There may be gaps: this is the nature of IDENTITY colums
In this case, learning to use the raw SQL INSERT will help: you have far more control over what you do. SET IDENTITY_INSERT will allow you to override the identity property

You could do this as a quick SQL Statement (assuming your first column is an IDENTITY column):
INSERT INTO dbo.YourTableName(class,rollnumber,age)
VALUES ('A', 10, 10),
('A', 11, 10),
('A', 12, 10)
This format is easy to copy and paste individual values into until you have all f them accounted for, then can be run once to put them in (or saved and rerun in other environments).
If the index column is not an IDENTITY column but is instead just a UNIQUE column that it is giving you an error on, you can specify it as a column and provide additional unique values like so:
INSERT INTO dbo.YourTableName(index, class,rollnumber,age)
VALUES (10, 'A', 10, 10),
(11, 'A', 11, 10),
(12, 'A', 12, 10)

If your Index column is a primary key with Identity, you could use the following to create N - 1 rows:
DECLARE #Counter int;
SET #Counter = 0;
WHILE (#Counter < 10)
BEGIN
INSERT INTO table ([class],[rollnumber],[age]) VALUES ('A',##IDENTITY + 1,10)
SET #Counter = #Counter + 1
END
This also assumes that rollnumber must be equal to your index.

Related

Not able to insert a row in a table which has auto incremented primary key

I have a table reportFilters which has the following column names:
The reportFilterId is auto increment. I want to insert a row in the table with the script below:
IF OBJECT_ID(N'ReportFilters', N'U') IS NOT NULL
BEGIN
IF NOT EXISTS (SELECT * FROM [ReportFilters]
WHERE ReportId IN (SELECT ReportId FROM [Reports] WHERE ReportType = 'Operational Insights Command Staff Dashboard') )
BEGIN
INSERT INTO [ReportFilters] Values(1, 'SelectView', 'Select Views', 13, 'Views','Views', 'SelectView', 'a', 'b', 'c' );
END
END
GO
But I am getting the following error:
Column name or number of supplied values does not match table definition.
Can I please get help on this ? Thanks in advance.
I think the problem is on inserted columns can't match with inserted data because that will instead by your table column order which is ReportFilterId instead of ReportId
So that there are 11 columns in your table but your statement only provides 10 columns.
I would use explicitly specify for inserted columns (inserted columns start from ReportId except your PK ReportFilterId column)
INSERT INTO [ReportFilters] (ReportId,ReportFilterName,ReportFilterTitle....)
Values (1, 'SelectView', 'Select Views', 13, 'Views','Views', 'SelectView', 'a', 'b', 'c' );

Tsql Inserting Unordered Sequence

I'm trying to insert an unordered sequence (list) into 2 kind of table explained here:
Table has one INT column
Table has several columns, one of them is an INT column (which I will use to insert the data)
So, here is the example;
List; (1, 3, 55, 3, 56456, 45)
Table; (Value INT)
I'm not looking for any solution, but a solution which is not including 'parse, split etc.' operations.
Expected solution:
INSERT INTO Table(Value)
VALUES(anyList)
Expected solution 2:
INSERT INTO Table(Value)
VALUELIST(anyList)
OR
INSERT INTO Table(Value)
VALUETABLE(anyList)
You can't directly insert like following.
INSERT INTO Table(Value)
VALUES (1, 3, 55, 3, 56456, 45)
You will get following error.
Msg 110, Level 15, State 1, Line 1 There are fewer columns in the
INSERT statement than values specified in the VALUES clause. The
number of values in the VALUES clause must match the number of columns
specified in the INSERT statement.
SQL Server will only accept your Insert if you give like following.
INSERT INTO Table(Value)
VALUES (1),(3),(55), (3), (56456), (45)
If you are getting the list as a string, in that case you need to convert it into appropriate format before inserting, like following.
DECLARE #xml as xml
DECLARE #list as varchar(max)
SET #list='(1, 3, 55, 3, 56456, 45)'
set #list = REPLACE(REPLACE(#list,'(',''),')','')
SET #xml = cast(('<X>'+replace(#list,',' ,'</X><X>')+'</X>') as xml)
INSERT INTO [Table](Value)
SELECT N.value('.', 'int') as value FROM #xml.nodes('X') as T(N)

Dumbfounded declaring a variable in sql

I have searched across the board and the internet trying to see what I'm doing wrong. I have endlessly gotten the "Declare: syntax" error no matter where in the code I have moved the declaration. I've tried it with and without # signs and semicolons thinking this was something different than C++. All the guides I've read say this should work so perhaps someone here can tell me what is wrong?
BEGIN TRANSACTION;
DECLARE #clID INT;
CREATE TABLE CLAIM(claimID, repDate, lossDate, claimNo, claimStat);
INSERT INTO CLAIM VALUES(1, '2016-10-1', '2016-10-1', 1, 'Open');
INSERT INTO CLAIM VALUES(2, '2016-10-1', '2016-10-1', 2, 'Open');
CREATE TABLE EXPOSURE(expID, claimID, coverage, claimEx, expStat);
INSERT INTO EXPOSURE VALUES(1, 2, 'BI', 'U152', 'Open');
INSERT INTO EXPOSURE VALUES(2, 2, 'PD', 'U152', 'Open');
CREATE TABLE RESERVELINE(resLineID, expID, claimID, covID, IP);
INSERT INTO RESERVELINE VALUES(1, 1, 2, 'BI', 02);
INSERT INTO RESERVELINE VALUES(1, 3, 3, 'CDL', 01);
CREATE TABLE RESULTS(claimID);
COMMIT;
INSERT INTO RESULTS
SELECT claimID FROM CLAIM WHERE repDate<'2016-10-3';
SELECT * FROM RESULTS;
--Must FIRST query first table with Date()
Thanks guys.

MS SQL INSERT INTO trouble

I need to insert new record in table.
The first column name ob_no is int, not null. So I need generate number which is maximum ob_no at the moment +1. How can I do it? Something Like (max(ob_no) + 1) but it doesn't work in SQL 2005. Thanks for any ideas.
INSERT INTO et_thanks_2014 (ob_no, c_name)
VALUES (???, 'Some Text')
You should use identities if you don't need values without lag:
INSERT INTO et_thanks_2014 (ob_no, c_name)
SELECT MAX(ob_no) + 1, 'Some Text'
FROM et_thanks_2014

Not in In SQL statement?

I have set of ids in excel around 5000 and in the table I have ids around 30000. If I use 'In' condition in SQL statment I am getting around 4300 ids from what ever I have ids in Excel. But If I use 'Not In' with Excel id. I have getting around 25000+ records. I just to find out I am missing with Excel ids in the table.
How to write sql for this?
Example:
Excel Ids are
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
Table has IDs
1,
2,
3,
4,
6,
8,
9,
11,
12,
14,
15
Now I want get 5,7,10 values from Excel which missing the table?
Update:
What I am doing is
SELECT [GLID]
FROM [tbl_Detail]
where datasource = 'China' and ap_ID not in (5206896,
5206897,
5206898,
5206899,
5117083,
5143565,
5173361,
5179096,
5179097,
5179150)
Try this:
SELECT tableExcel.ID
FROM tableExcel
WHERE tableExcel.ID NOT IN(SELECT anotherTable.ID FROM anotherTable)
Here's an SQL Fiddle to try this: sqlfiddle.com/#!6/31af5/14
You're probably looking for EXCEPT:
SELECT Value
FROM #Excel
EXCEPT
SELECT Value
FROM #Table;
Edit:
Except will
treat NULL differently(NULL values are matching)
apply DISTINCT
unlike NOT IN
Here's your sample data:
declare #Excel Table(Value int);
INSERT INTO #Excel VALUES(1);
INSERT INTO #Excel VALUES(2);
INSERT INTO #Excel VALUES(3);
INSERT INTO #Excel VALUES(4);
INSERT INTO #Excel VALUES(5);
INSERT INTO #Excel VALUES(6);
INSERT INTO #Excel VALUES(7);
INSERT INTO #Excel VALUES(8);
INSERT INTO #Excel VALUES(9);
INSERT INTO #Excel VALUES(10);
declare #Table Table(Value int);
INSERT INTO #Table VALUES(1);
INSERT INTO #Table VALUES(2);
INSERT INTO #Table VALUES(3);
INSERT INTO #Table VALUES(4);
INSERT INTO #Table VALUES(6);
INSERT INTO #Table VALUES(8);
INSERT INTO #Table VALUES(9);
INSERT INTO #Table VALUES(11);
INSERT INTO #Table VALUES(12);
INSERT INTO #Table VALUES(14);
INSERT INTO #Table VALUES(15);
Import your excel file into SQL Server using the Import Data Wizard found in SQL Server Management Studio.
Then you can write the following query to find any IDs which are in the file but not in the table:
SELECT id
FROM imported_table
WHERE id NOT IN (SELECT id FROM db_table)
You should move excel data to a table in SQL Server, and then do the query in SQL Server.
select distinct id from Excel where id not in (select your ids from Sqltable)
(Obviously select your ids from Sqltable is a select which returns the Ids existing on SQL Server).
You may think that moving data to SQL Server is hard to do, but, on the contrary, it's very easy:
1) create a table
CREATE TABLE ExcelIds (Id int)
2) add a new column in excel with the following formula:
="insert into ExcelIds values(" & XX & ")"
where XX is the reference to the cell in the column with excel Ids.
3) copy the "inserts" from Excel into SSMS or whatever tool you're usin in SQL Server, and execute them.
Now you have 2 tables in SQL Server, so that querying it is absolutely easy.
When you're over, just drop the table
DROP TABLE ExcelIds
NOTE: I didn't create a key on SQL Server table because I suppose that the Ids can be repeated. Neither is justified to create a more complex SQL Query to avoid duplicates in ExcelIds for this ad hoc solution.