Insert Syntax in SQL: Without INTO and VALUE - sql-server

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 ')'.

Related

Getting below error while doing scd2 in snowflake using merge statement

My statement:
insert into target_scd2
select id,name,flag from (
merge into target_scd2 as t
using source as s
on t.id=s.id
when matched and t.name <> s.name then
update set flag='N'
when not matched then
insert values(s.id,s.name,'Y')
OUTPUT $Action action_flag,s.id,s.name,'Y'
) as merge_out
where merge_out.action_flag='UPDATE';
I am getting below error while executed the above statement:
SQL compilation error: syntax error line 3 at position 0 unexpected 'merge'. syntax error line 9 at position 29 unexpected ')'.
Can you please help what can be the issue
The pattern presented here is called: "INSERT over DML" and it is SQL Server specific.
More info: D. Inserting the results of the MERGE statement into another table
Can you please help what can be the issue
a) Using merge as subquery of INSERT SELECT
b) OUTPUT/(RETURNING) clause support
The Snowflake documentation of MERGE.

Problem in snowflake when using expression on values clause for an INSERT statement

Hi I am having this problem with Snowflake.
I have some insert statement like these inside an Snowflake SP:
DROP TABLE TABLE1;
CREATE table TABLE1 (COLUMN1 VARCHAR(10));
insert into table1 values('TEST' || TRIM(0));
When I execute those statements I get an error like this:
SQL compilation error: Invalid expression ['TEST' || '0'] in VALUES clause
Is there a documented limitation for the VALUES clause in snowflake. Any kind of work around for this?
Per the documentation here: https://docs.snowflake.com/en/sql-reference/constructs/values.html
Each expression must be a constant, or an expression that can be
evaluated as a constant during compilation of the SQL statement.
Most simple arithmetic expressions and string functions can be
evaluated at compile time, but most other expressions cannot.
In looking at your example, you are using a TRIM on a numeric expression, which isn't necessary. If you remove the TRIM(), then your example works fine.
I found a workaround for the limitation that #mike-walton described.
You can chance:
insert into table1 values('TEST' || TRIM(0));
to
insert into table1 select 'TEST' || TRIM(0);
And it will work

My stored procedure in SQL Server 2008 when is execute with JOB its fails, but without its working great

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

sql-c#-insert data to database and output autoincreament id

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')

Sql Stored Procedures

I'm trying to write a Stored Procedure this is what I have so far
Create procedure sp_Create_order
#P_nafn varchar(50),
#P_fj int,
#P_sótt datetime,
#F_kt varchar(10),
#V_nr int,
#L_id int
as
begin
set nocount on
if exists(
   select * from Lotur
   where L_id=#L_id and
   #P_sótt between L_hefst and L_pfrest
)
INSERT INTO Pantar(P_nafn, P_fj, P_sótt, F_kt, V_nr, L_id)
VALUES (#P_nafn, #P_fj, #P_sótt, #F_kt, #V_nr, #L_id)
end
but I am getting these errors
Msg 102, Level 15, State 1, Procedure
sp_Create_order, Line 14 Incorrect
syntax near ' '.
Msg 102, Level 15, State 1, Procedure
sp_Create_order, Line 15 Incorrect
syntax near ' '.
on these lines
select * from Lotur
where L_id=#L_id
and
#P_sótt, L_hefst and L_pfrest
are all dates and I am tryng to put a condition on saying that nothing should be Inserted unless #P_sótt is equal to or between L_hefst and L_pfrest
Please use meaningful names for your variables
Do not create sp for every thing like the one above
Modify the query to have SELECT L_ID NOT SELECT *
As for the error, probably you have mistyped something
First of all, I wouldn't recommend prefixing your Stored Procedure with sp_. Performance is at least one reason why it's a bad idea.
Your stored procedure compiled ok for me. Another recommendation would be to use
if exists (
select 1
from
Lotur
where
L_id= #L_id
and #P_sótt between L_hefst and L_pfrest
)
as opposed to SELECT *. Did you get the error when compiling or trying to use it?
EDIT:
In response to your post about raising errors, you might want to look at RAISERROR. Here is an example of how to use it in Stored procedures

Resources