Insert query with gettime() into SQL Server table using Python - sql

I am having an issue putting the below SQL query into Python :
insert into GERVERSION values (10, 'xxxxxxxx', 7, 4, 2, getdate(),'')
Note that in SQL , I have timestamp for the getdate() part
Can anyone please help me out with the right syntax to put this into work ?

Try using INSERT INTO ... SELECT syntax:
INSERT INTO GERVERSION
SELECT 10, 'xxxxxxxx', 7, 4, 2, GETDATE(), '';

Related

MariaDb Begin End in a multi-value Insert

Im using MariaDb and Im trying to execute a multi-value insert query using Begin End and I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 1 0.000 sec
BEGIN;
INSERT INTO Client (Id, Name, Total)
VALUES
(1, 'John', 1000),
(2, 'Mary', 3000),
(3, 'Mike', 14000),
(4, 'George', 15000);
END;
Can I use Begin, End with a multi-value insert and if yes what is the correct syntax here?
I have millions of data to insert. the above is just a small sample of my dataset

Convert function SQL Server

I'm trying to convert the data of a column from varchar(255) to Timestamp.
I went to Microsoft documentation and it's not working
Code:
SELECT
["Data Nascimento"]
FROM
[leoninos] AS Original,
CONVERT(VARCHAR, ["Data Nascimento"]) AS VARCHAR,
CONVERT(timestamp(6, 4), ["Data Nascimento"]) AS timestamp;
Error:
Msg 156, Level 15, State 1, Line 53.
Incorrect syntax next to keyword 'CONVERT'.
What is the syntax error?
Thanks in advance.
Don't use varchar and timestamp as column aliases. Try this:
SELECT ["Data Nascimento"] as original
TRY_CONVERT(varchar(255), ["Data Nascimento"]) AS type_varchar
-- TRY_CONVERT(timestamp(6, 4), ["Data Nascimento"]) AS type_timestamp
FROM [leoninos] ;
Notes:
timestamp isn't really appropriate. I don't know what you are trying to do.
The FROM clause goes after the SELECT list.
Don't use SQL keywords as column names (even if they are not reserved).
Use try_convert() in case the conversion fails.
It's hard to tell what you are doing- but it appears that you are trying to select a table and also two scalars at once. Try just doing the CONVERT statements on their own with sample values:
SELECT CONVERT(int, '10'), CONVERT(date, '20180720')
Then, once you have it working for some sample values, you can convert column values:
CREATE TABLE #test (a NVARCHAR(max))
INSERT INTO #test VALUES ('january 1 2018')
INSERT INTO #test VALUES ('20180720')
SELECT a, CONVERT(date, a) FROM #test
DROP TABLE #test

Insert several values in SQL Server

I'm trying to insert some values into SQL Server but I get an error message:
Line 2: Incorrect syntax near ','
I need to enter several values in 1 table so I was looking for a quicker way to not insert 1 at a time.
For some reason I always have issues with databases :(
My query is:
INSERT INTO PERSONALRH_NIVEL (SERHGCCAB, PUESTO_ID, COMPANIA_ID, REGION_ID, TIPO_EMPLEADO)
VALUES (81570, 4, 2001, 2, 'N'),
(81570, 4, 2001, 3, 'S'),
(81570, 4, 2001, 3, 'N');
Thank you in advance, David
For older versions of SQL Server you can use insert from select
INSERT INTO PERSONALRH_NIVEL (SERHGCCAB, PUESTO_ID, COMPANIA_ID, REGION_ID, TIPO_EMPLEADO)
SELECT 81570, 4, 2001, 2, 'N'
UNION ALL
SELECT 81570, 4, 2001, 3, 'S'
UNION ALL
SELECT 81570, 4, 2001, 3, 'N';
It depends on the version of SQL Server you are using. Insert Into () VALUES (),() only works in SQL Server 2008 and newer. If you are using SQL Server 2005 you need to use separate Insert statements.

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.

SQL Server – inserting multiple rows with single (ANSI style) statement

I am using following method for inserting multiple rows using a single INSERT statement, that is the ANSI style of inserting rows. It is available in SQL Server 2008 and 2012. I am not sure of SQL Server 2005/ 2000.
Create test table:
create table TestInsert (ID INT, Name NVARCHAR(50))
Single INSERT statement to insert 5 rows
INSERT INTO TestInsert
VALUES (1,'a'),
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
Please let me know if there is any other best way to achieve this
Try this instead:
INSERT TestInsert
SELECT 1, 'a'
UNION ALL
SELECT 2, 'b'
UNION ALL
SELECT 3, 'c'
UNION ALL
SELECT 4, 'd'
UNION ALL
SELECT 5, 'e'
SQL Server - inserting multiple rows with single (ANSI style) statement
For SQL Server 2000+
According to SQL The Complete Reference, Third Edition (August 12, 2009):
1) The syntax for multirow INSERTs is
INSERT INTO table-name (columns not mandatory)
query
(page 236, Figure 10-3).
2) The SELECT statement has the FROM clause mandatory (page 87, Figure 6-1).
So, in this case, to insert multiple rows using just one INSERT statement we need an auxiliary table with just one row:
CREATE TABLE dual(value INT PRIMARY KEY CHECK(value = 1))
INSERT dual(value) VALUES(1)
and then
INSERT INTO table-name (columns) -- the columns are not mandatory
SELECT values FROM dual
UNION ALL
SELECT another-values FROM dual
UNION ALL
SELECT another-values FROM dual
Edit 2: For SQL Server 2008+
Starting with SQL Server 2008 we can use row constructors: (values for row 1), (values for row 2), (values for row 3), etc. (page 218).
So,
INSERT INTO TestInsert
VALUES (1,'a'), --The string delimiter is ' not ‘...’
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
will work on SQL Server 2008+.