Sql Stored Procedures - sql-server

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

Related

How to fix a stored procedure that can recover deleted data

I have lately stumbled upon a blog post that talks about a stored procedure called Recover_Deleted_Data_Proc.sql that can apparently recover your deleted data from the .log file.
There is nothing new under the sun, we are going to use fn_dblog.
STEPS TO REPRODUCE
We are first going to create the table:
--Create Table
CREATE TABLE [Test_Table]
(
[Col_image] image,
[Col_text] text,
[Col_uniqueidentifier] uniqueidentifier,
[Col_tinyint] tinyint,
[Col_smallint] smallint,
[Col_int] int,
[Col_smalldatetime] smalldatetime,
[Col_real] real,
[Col_money] money,
[Col_datetime] datetime,
[Col_float] float,
[Col_Int_sql_variant] sql_variant,
[Col_numeric_sql_variant] sql_variant,
[Col_varchar_sql_variant] sql_variant,
[Col_uniqueidentifier_sql_variant] sql_variant,
[Col_Date_sql_variant] sql_variant,
[Col_varbinary_sql_variant] sql_variant,
[Col_ntext] ntext,
[Col_bit] bit,
[Col_decimal] decimal(18,4),
[Col_numeric] numeric(18,4),
[Col_smallmoney] smallmoney,
[Col_bigint] bigint,
[Col_varbinary] varbinary(Max),
[Col_varchar] varchar(Max),
[Col_binary] binary(8),
[Col_char] char,
[Col_timestamp] timestamp,
[Col_nvarchar] nvarchar(Max),
[Col_nchar] nchar,
[Col_xml] xml,
[Col_sysname] sysname
)
And we then insert data into it:
--Insert data into it
INSERT INTO [Test_Table]
([Col_image]
,[Col_text]
,[Col_uniqueidentifier]
,[Col_tinyint]
,[Col_smallint]
,[Col_int]
,[Col_smalldatetime]
,[Col_real]
,[Col_money]
,[Col_datetime]
,[Col_float]
,[Col_Int_sql_variant]
,[Col_numeric_sql_variant]
,[Col_varchar_sql_variant]
,[Col_uniqueidentifier_sql_variant]
,[Col_Date_sql_variant]
,[Col_varbinary_sql_variant]
,[Col_ntext]
,[Col_bit]
,[Col_decimal]
,[Col_numeric]
,[Col_smallmoney]
,[Col_bigint]
,[Col_varbinary]
,[Col_varchar]
,[Col_binary]
,[Col_char]
,[Col_nvarchar]
,[Col_nchar]
,[Col_xml]
,[Col_sysname])
VALUES
(CONVERT(IMAGE,REPLICATE('A',4000))
,REPLICATE('B',8000)
,NEWID()
,10
,20
,3000
,GETDATE()
,4000
,5000
,getdate()+15
,66666.6666
,777777
,88888.8888
,REPLICATE('C',8000)
,newid()
,getdate()+30
,CONVERT(VARBINARY(8000),REPLICATE('D',8000))
,REPLICATE('E',4000)
,1
,99999.9999
,10101.1111
,1100
,123456
,CONVERT(VARBINARY(MAX),REPLICATE('F',8000))
,REPLICATE('G',8000)
,0x4646464
,'H'
,REPLICATE('I',4000)
,'J'
,CONVERT(XML,REPLICATE('K',4000))
,REPLICATE('L',100)
)
GO
We are now going to verify if the data are there:
--Verify the data
SELECT * FROM Test_Table
At this point we need to create the stored procedure. I couldn't paste it here because it's too long but you can download it from the same blog post there is a link to a Box file.
If the query gives you troubles like this:
Msg 50000, Level 16, State 1, Procedure Recover_Deleted_Data_Proc, Line 22 [Batch Start Line 700] The compatibility level should be equal to or greater SQL SERVER 2005 (90)
Msg 50000, Level 16, State 1, Procedure Recover_Deleted_Data_Proc, Line 22 [Batch Start Line 705] The compatibility level should be equal to or greater SQL SERVER 2005 (90)
Is because you have to comment out from line 701 to line 708.
Cool, let's now delete the data from that table:
--Delete the data
DELETE FROM Test_Table
And confirm that the data were deleted:
--Verify the data
SELECT * FROM Test_Table
And here is the last step: we need to try to recover the data using the freshly installed stored procedure.
The author instruct us to use one of these two commands (don't forget to change 'test' with the name of your database):
--Recover the deleted data without date range
EXEC Recover_Deleted_Data_Proc 'test', 'dbo.Test_Table'
or
--Recover the deleted data it with date range
EXEC Recover_Deleted_Data_Proc 'test', 'dbo.Test_Table', '2012-06-01', '2012-06-30'
But the problem is that both returns this error:
(8 rows affected)
(2 rows affected)
(64 rows affected)
(2 rows affected)
(1 row affected)
(1 row affected)
(1 row affected)
(1 row affected)
(1 row affected)
(1 row affected)
Msg 245, Level 16, State 1, Procedure Recover_Deleted_Data_Proc, Line 485 [Batch Start Line 112]
Conversion failed when converting the varchar value '0x41-->01 ; 0001' to data type int.
If I right click on the stored procedure and I click "Modify", I don't see anything particularly fishy at Line 485.
Any idea why this stored procedure is not working?
What is the conversion mentioned?
The code is 10 years old and was written with the assumption that a [PAGE ID] would only ever be expressed as a pair of integers, e.g. 0001:00000138 - however, as you have learned, sometimes that is expressed differently, like 0x41-->01 ; 0001:00000138.
You can fix that problem by adding this inside the cursor:
IF #ConsolidatedPageID LIKE '0x%-->%;%'
BEGIN
SET #ConsolidatedPageID = LTRIM(SUBSTRING(#ConsolidatedPageID,
CHARINDEX(';', #ConsolidatedPageID) + 1, 8000));
END
But then your next problem is when you saved the procedure from the box file it probably changed '†' to some wacky ? character. When I fixed that (using N'†' of course, since Unicode characters should always have N), I still got these error messages:
Msg 537, Level 16, State 3, Procedure Recover_Deleted_Data_Proc, Line 525
Invalid length parameter passed to the LEFT or SUBSTRING function.
Msg 9420, Level 16, State 1, Procedure Recover_Deleted_Data_Proc, Line 651
XML parsing: line 1, character 2, illegal xml character
After 15 minutes of trying to reverse engineer this spaghetti, I gave up. If you need to recover data you deleted, restore a backup. If you don't have a backup, well, that's why we take backups. The fragile scripts people try to create to compensate for not taking backups are exactly why log recovery vendors charge the big bucks.
As an aside, the compatibility level error message is a red herring, totally misleading as the logic is currently written, and completely irrelevant to the problem. But it can be solved if, right before this:
IF ISNULL(#Compatibility_Level,0)<=80
BEGIN
RAISERROR('The compatibility level should ... blah blah',16,1)
RETURN
END
You add this:
IF DB_ID(#Database_Name) IS NULL
BEGIN
RAISERROR(N'Database %s does not exist.',11,1,#Database_name);
RETURN;
END
Or simply not calling those two example calls at the end of the script, since they depend on you having a database called test, which clearly you do not.

How can I use a where statement in my procedure with open quotes inside the select statement already?

In my procedure, I need to have input parameters for the table and I need to be able to make multiple select statements into my procedure. However when I begun to add WHERE conditions, they only worked on numerical conditions i.e. where number = 1, but it doesn't work for text = 'a' as the statement already opens with ' ' i.e. 'select...' (see the code)
So the text conditions conflicts with the opening quote mark.
So how do I add this where statement with regards to text?
(The problem is where date is)
The output is the following:
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near '00'.
(53 rows affected)
I've tried to separate the where clause with separate ' ' but no use.
I've tried to use " inside the ' ' but no use, and says 'column name not recognised'
I've tried to treat the text as numeric without ' ' but no use.
CREATE PROCEDURE AllRowsAndCount35
    #table1 NVARCHAR(128)
AS
BEGIN
    DECLARE #SafeTableName AS NVARCHAR(128)
    SELECT #SafeTableName = QUOTENAME(TABLE_NAME)
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_NAME = #table1
    DECLARE #sql AS NVARCHAR(MAX) = 'select count(*) as countrows from ' + #SafeTableName + 'where valid_from_etl = 2019-06-24 00:00:00.000'
    EXEC(#SQL)
DECLARE #sql2 AS NVARCHAR(MAX) = 'select * from ' + #SafeTableName
    EXEC(#SQL2)
END
EXEC AllRowsAndCount35 'Dim_Cluster'
I would like a result showing a count of rows, another separate result showing the entire table
Basically two separate queries based off the procedure where I enter the table name as an input parameter
Instead the error message is:
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near '00'.
Use two single quotes (not a double quote) to represent a single quote inside a quoted string.
For example: 'SELECT... WHERE date = ''date'''
Or better yet, use a variable like you are for #safetablename.
That all said, your proc won't return both data sets. You'll have to use a UNION to make 1 final select with all the data you want returned, or use parameters to define which query is executed when the proc is called and call the proc twice, or make two separate procs.

Does SQL Server deferred name resolution work for functions?

SQL Server has Deferred Name Resolution feature, read here for details:
https://msdn.microsoft.com/en-us/library/ms190686(v=sql.105).aspx
In that page, all it's talking is stored procedure so it seems Deferred Name Resolution only works for stored procedures and not for functions and I did some testing.
create or alter function f2(#i int)
returns table
as
return (select fff from xxx)
go
Note the table xxx does not exist. When I execute the above CREATE statement, I got the following message:
Msg 208, Level 16, State 1, Procedure f2, Line 4 [Batch Start Line 22]
Invalid object name 'xxx'.
It seems that SQL Server instantly found the non-existent table xxx and it proved Deferred Name Resolution doesn't work for functions. However when I slightly change it as follows:
create or alter function f1(#i int)
returns int
as
begin
declare #x int;
select #x = fff from xxx;
return #x
end
go
I can successfully execute it:
Commands completed successfully.
When executing the following statement:
select dbo.f1(3)
I got this error:
Msg 208, Level 16, State 1, Line 34
Invalid object name 'xxx'.
So here it seems the resolution of the table xxx was deferred. The most important differences between these two cases is the return type. However I can't explain when Deferred Name Resolution will work for functions and when not. Can anyone help me to understand this? Thanks in advance.
It feels like you were looking for understanding of why your particular example didn't work. Quassnoi's answer was correct but didn't offer a reason so I went searching and found this MSDN Social answer by Erland Sommarskog. The interesting part:
However, it does not extend to views and inline-table functions. For
stored procedures and scalar functions, all SQL Server stores in the
database is the text of the module. But for views and inline-table
functions (which are parameterised view by another name) SQL Server
stores metadata about the columns etc. And that is not possible if the
table is missing.
Hope that helps with understanding why :-)
EDIT:
I did take some time to confirm Quassnoi's comment that sys.columns as well as several other tables did contain some metadata about the inline function so I am unsure if there is other metadata not written. However I thought I would add a few other notes I was able to find that may help explain in conjunction.
First a quote from Wayne Sheffield's blog:
In the MTVF, you see only an operation called “Table Valued Function”. Everything that it is doing is essentially a black box – something is happening, and data gets returned. For MTVFs, SQL can’t “see” what it is that the MTVF is doing since it is being run in a separate context. What this means is that SQL has to run the MTVF as it is written, without being able to make any optimizations in the query plan to optimize it.
Then from the SQL Server 2016 Exam 70-761 by Itzik Ben-Gan (Skill 3.1):
The reason that it's called an inline function is because SQL Server inlines, or expands, the inner query definition, and constructs an internal query directly against the underlying tables.
So it seems the inline function essentially returns a query and is able to optimize it with the outer query, not allowing the black-box approach and thus not allowing deferred name resolution.
What you have in your first example is an inline function (it does not have BEGIN/END).
Inline functions can only be table-valued.
If you used a multi-statement table-valued function for you first example, like this:
CREATE OR ALTER FUNCTION
fn_test(#a INT)
RETURNS #ret TABLE
(
a INT
)
AS
BEGIN
INSERT
INTO #ret
SELECT a
FROM xxx
RETURN
END
, it would compile alright and fail at runtime (if xxx would not exist), same as a stored procedure or a scalar UDF would.
So yes, DNR does work for all multi-statement functions (those with BEGIN/END), regardless of their return type.

Conversion error with simple SQL UPDATE statement

I have an application that is using SQL Server 2016 for backend storage. I have the following statement I'm trying to run:
UPDATE dbo.AspNetUsers
SET [IsActive] = 1
WHERE [Id] = '1b08b7a9-2978-4116-8a9c-e86bc9ae8bbf'
[IsActive] is a BIT column
[Id] is a NVARCHAR(128) column
When executing, I get the following error:
Msg 245, Level 16, State 1, Procedure tUserActiveChange, Line 21 [Batch Start Line 4]
Conversion failed when converting the nvarchar value '1b08b7a9-2978-4116-8a9c-e86bc9ae8bbf' to data type int.
Seems like a pretty straightforward update statement. I even tried the same type of statement on another database on the same server where the updated column is a BIT and using a VARCHAR column for the WHERE condition and it worked just fine.
What the heck?
Well, I realized I had a trigger I created a while back that is causing the error (tUserActiveChange). The trigger updates another table when IsActive changes. It was, in fact, trying to assign a VARCHAR variable to an int field.
I fixed that and now the UPDATE statement is working fine. Thank you all for taking the time to look into this. I feel silly now haha.

CREATE PROCEDURE in NetBeans IDE

I just wanted to add a simple procedure in Account table in Bank_Admin Schema.
The procedure adds some amount of money to the existing balance an account holder has. I guess, by reading the Sql command, you will figure it out.
But I am getting this error:
[Exception, Error code 30,000, SQLState 42X01] Syntax error:
Encountered "BEGIN" at line 2, column 1.
Line 2, column 1
Execution finished after 0.036 s, 1 error occurred
.Please help me with this code...
CREATE PROCEDURE deposit_in_bank (IN bank_account int, IN deposit_amount int)
BEGIN
UPDATE BANK_ADMIN.ACCOUNT SET balance = balance + deposit_amount WHERE account_no = bank_account
END
I'm assuming that defining a SQL stored procedure in NetBeans is the same as doing so directly as a SQL statement. If so...
I don't think your IN keywords are valid, all parameters are presumed to be input parameters unless specified as an output parameter.
As scsimon points out, you also need AS before your BEGIN.
I don't think you actually need your BEGIN and END at all either. Or the brackets around your parameters.
And I don't think you're writing your parameter variables correctly (they should be prefixed with #, so the following should work:
CREATE PROCEDURE deposit_in_bank
#bank_account INT,
#deposit_amount INT
AS
UPDATE BANK_ADMIN.ACCOUNT
SET balance = balance + #deposit_amount
WHERE account_no = #bank_account

Resources