Dumbfounded declaring a variable in sql - 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.

Related

EXPLAIN Failed. 3706: (-3706)Syntax error: expected something between ')' and ','

I've seen this error mentioned on StackOverflow, but not in the context I am using. I am relatively new to Teradata and this behavior is throwing me for a loop. Here is code that works:
INSERT INTO test_table (this, that) VALUES (1, 2);
Here is code that throws the error:
INSERT INTO test_table (this, that) VALUES (1, 2), (3, 4);
This is super confusing to me because the Teradata docs have the following example:
INSERT INTO cities VALUES (2, 'San Jose'), (3, 'Oakland');
Could someone show me what am I missing here? Thanks!
Teradata only allows you to insert one record with a single values. You can see this in the syntax diagram for insert -- there is no "backloop".
Two inserts is a simple workaround:
INSERT INTO test_table (this, that)
VALUES (1, 2);
INSERT INTO test_table (this, that)
VALUES (3, 4);

Load many difficult objects in hive

I have old version of hive, below hive 2.0, in this hive version i create a table:
create table test (int id, string name, values array<string>)
When i add data, i use this query:
insert into table test select 1, 'Sam', array('sql', 'c++', 'c#', 'java')
It work, but i need load more then one row in table, like:
insert into table select (1, 'Sam', array('sql', 'c++', 'c#', 'java')), (2, 'AN Other', array('paskal'))
How i can do this?
Hive doesn't support use UDFs in something like this
INSERT INTO TABLE test VALUES (1, 'Sam', array('sql', 'c++', 'c#', 'java')), (2, 'test1', array('paskal'));
you will get something like
Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values
A workaround would be something like this
INSERT INTO TABLE test
select 1, 'Sam', array('sql', 'c++', 'c#', 'java')
union all
select 2, 'test1', array('paskal');

What's wrong with this SQL INSERT statement?

I want to insert a certain value into a certain field of five different rows altogether. But whenever I run this query it doesn't get executed. What's wrong with it, and how can I fix it?
INSERT INTO `employee`(`password`) VALUES ('abc') WHERE `id` IN (1,2,3,4,5);
An INSERT can't have a WHERE clause. It looks like you meant to do an update:
UPDATE `employee`
SET `password` = 'abc'
WHERE `id` IN (1,2,3,4,5);
Or perhaps a multi-row insert:
INSERT INTO `employee` (`id`, `password`)
VALUES (1, 'abc'), (2, 'abc'), (3, 'abc'), (4, 'abc'), (5, 'abc');
Also, FYI, you really shouldn't be storing passwords as plain text, which it looks like you may be doing.

Trying to add 10 values to sql database [closed]

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.

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.