I want to get Id from sql, this is my sql:
INSERT INTO Respondent (Gender) VALUES ('Male') OUTPUT inserted.Id.
error: Incorrect syntax near 'OUTPUT'.
I think you are using MySQL and so the error. Use LAST_INSERT_ID() instead like
INSERT INTO Respondent (Gender) VALUES ('Male');
select LAST_INSERT_ID();
If it's SQL Server then your syntax is wrong. See OUTPUT Clause. change your query to
INSERT INTO Respondent (Gender)
OUTPUT inserted.Id
VALUES ('Male')
Related
I have tried the following queries -
INSERT INTO (:tableName) (ENTITY_NAME,PAYLOAD_BLOB,PAYLOAD_JSON) VALUES (:entityName,:payload_blob,:payload_json);
INSERT INTO #vars.tableName (ENTITY_NAME,PAYLOAD_BLOB,PAYLOAD_JSON) VALUES (:entityName,:payload_blob,:payload_json);
INSERT INTO $(vars.tableName) (ENTITY_NAME,PAYLOAD_BLOB,PAYLOAD_JSON) VALUES (:entityName,:payload_blob,:payload_json);
But all the 3 queries resulted in an Bad Synatx error.
Create variable with SQL statement
INSERT INTO ${vars.tableName} (ENTITY_NAME,PAYLOAD_BLOB,PAYLOAD_JSON) VALUES (:entityName,:payload_blob,:payload_json)
then use it in the Insert Component. Keep in mind - you still have to provide these internal values :entityName,:payload_blob,:payload_json in this Insert Component.
I'm understanding a stored procedure, here's part of the code:
INSERT [dbo].[PartitionMaintenanceTables] (
nvc_TableSchema,
nvc_TableName,
i_CompressInterval,
vc_CompressType,
i_RetainInterval,
dt_CreatedDatetime,
dt_ChangedDatetime,
dt_DeletedDatetime,
ti_NeedsRepl,
nvc_ChangedDatabaseName
)
As you can see, it's not inserting any value into this table. What does it mean? Is it inserting a bunches of default / null values to them?
Syntax like:
INSERT tab(col); -- is invalid
INTO is optional. As for missing VALUES clause you probably have SELECT after insert:
INSERT tab(col) SELECT ...;
db<>fiddle demo
That code by itself will generate a syntax error:
Incorrect syntax near ')'.
I have this stored procedure wich its like this :
ALTER PROCEDURE [dbo].[P_ALIMENTATION_VolumeVentes]
AS
BEGIN
SELECT EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS, SecteurNomEDS,DirectionEDS,DirectionNomEDS,
(SELECT count(*) FROM CPListeVentesNonConformes WHERE CPListeVentesNonConformes.EDS = CPRT.EDS AND TypePart='PP') AS ListeVenteNC_PP,
(SELECT count(*) FROM CEListeVentesNonConformes WHERE CEListeVentesNonConformes.EDS = CPRT.EDS AND TypePart='ET') AS ListeVenteNC_ET,
(SELECT count(*) FROM CPListeVentesNonConformes WHERE CPListeVentesNonConformes.EDS = CPRT.EDS AND TypePart='PP' OR TypePart='ET') AS ListeVenteNC_PPET,
(SELECT count(*) FROM ListeVentes WHERE IDES01 = CPRT.EDS AND TypePart='PP') AS ListeVentes
INTO VolumeVentes
FROM CPR CPRT
GROUP BY EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS, SecteurNomEDS,DirectionEDS,DirectionNomEDS,TypePart
END
When i execute with the command line EXEC [dbo].[P_ALIMENTATION_VolumeVentes]
that work super great my table is create.
But when i use SQL Agent to schedule a job i have a nice surprise to have this error :
Executed as user: ZRES\CSAPREP10IUCRADM. The conversion of a varchar
data type to a datetime data type resulted in an out-of-range value.
[SQLSTATE 22007] (Error 242) The statement has been terminated.
[SQLSTATE 01000] (Error 3621). The step failed.
The structure table who will be create VolumetVentes have no fields with a type as datetime
Here is the structure of the tableVolumeVentes
I don't understand exactly where is the error ?
Thank you for help
Actually it should never work since you already have VolumeVente table.
SELECT INTO creates new table with columns described in select statement
https://msdn.microsoft.com/en-us/library/ms188029(v=sql.120).aspx
You should modify this code to become INSERT SELECT.
But you will probably still get the same conversion error because (I guess) column order is not correct in select statment and does not match column order in existing table. That is why you should always explicitly define column list in INSERT INTO clause, so the final script will look like:
INSERT INTO VolumeVentes(EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS, ...)
SELECT EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS
FROM ...
Use
INSERT INTO... Statement
instead of
SELECT INTO FROM... Statement
I have two different databases. I have tried using an UPDATE query, but it comes back with an unrecognised error .
How can I reference the DB in the SQL query.
The location is like this for both:
SERVER01\ABC.DB1
And
SERVER01.DB2
EDIT 1:
I have tried
insert into [DB1].[dbo].[table1]
select col1 from [ABC.DB2].[dbo].[table2]
But, I get this error,
Invalid object name 'ABC.DB2.dbo.table2'.
You can specify the database name in the query:
insert into [DbName1].[dbo].[Table1]
select * from [ABC.DB2].[dbo].[Table1]
Database : SQL Server 2008
Is there a way to have an insert statement return a value in SQL Server,
There is a similar question in
Return a value from a insert statement , which talks about ORACLE .
I don't work with databases much, and not aware of ORACLE/SQL Server much, so sorry to ask it all over again.
For simplicity let me provide a small example :
The table EmpDetails has EmpiD and EmpName. The value of EmpID is autogenerated by the database.
INSERT INTO EMPDETAILS VALUES("John")
Now I want to get the value of EmpID associated with John
I don't want a stored procedure, I want a SQL statement only .
Yes - you can use the little known and little used OUTPUT clause in your INSERT statement
INSERT INTO dbo.YourTable(col1, col2, col3, ...., ColN)
OUTPUT Inserted.Col1, Inserted.Col5, Inserted.ColN
VALUES(val1, val2, val3, ....., valN)
This returns a normal set of data, that you can deal with as you need to.
As the MSDN docs show, you can also send the OUTPUT values into e.g. a table variable or temp table for later use, if you need to.
To answer your updated question, use this:
INSERT INTO dbo.EMPDETAILS(EmpName)
OUTPUT Inserted.EmpID
VALUES("John")