How to start user procedure in sql? - sql

I have a SQL Server function which I need to exec. But I tried to call it via select, exec - it throws an error. How to fix that bug and launch that function?
Procedure code
create or alter function mergetext
(#TitleOfCourtesy text,
#FirstName text,
#LastName text)
returns varchar(255)
as
begin
return concat(#TitleOfCourtesy, ' ', #FirstName, ' ', #LastName)
end
select mergetext('a','b','c')
Error message
Msg 156, Level 15, State 1, Procedure mergetext, Line 12 [Batch Start Line 0]
Incorrect syntax near the keyword 'select'.
Msg 195, Level 15, State 10, Procedure mergetext, Line 12 [Batch Start Line 0]
'mergetext' is not a recognized built-in function name.

When calling a SQL Server function, you need to include the schema. If you did not specify a schema when creating the function, then the function is created using your default schema (probably dbo). So try calling the function so:
SELECT dbo.mergetext('a','b','c')

First of all, this is not procedure. It's a function.
Second, the text type is deprecated and should not be used.
Then, you need to separate the creation of the object either using GO, executing it in a separate query window or selecting only the function and pressing F5.
Try this:
create or alter function mergetext(
#TitleOfCourtesy text,
#FirstName text,
#LastName text
)
returns varchar(255)
as
begin
return concat(#TitleOfCourtesy,' ',#FirstName,' ',#LastName)
end
GO
select dbo.mergetext('a','b','c')
and finally, why you are creating a wrapper of the CONCAT function. Just use the CONCAT wherever you need.

Related

Stored procedure with table valued parameter

I am trying to make a stored procedure that uses a table as input parameter. However it is not working as intended. Steps I've taken:
First I make a user defined table type:
CREATE TYPE y_yhat_tabletype AS TABLE
(
UTC_DT datetime,
y float,
yhat float
);
Then I try to make a stored procedure:
create procedure sp_evaluate_y_yhat
#data y_yhat_tabletype readonly
as
begin
select sum(#data.y) as sum_y, sum(#data.yhat) as sum_yhat
end
But I get the error:
Msg 137, Level 16, State 1, Procedure sp_evaluate_y_yhat, Line 12 [Batch Start Line 0]
Must declare the scalar variable "#data".
Msg 137, Level 16, State 1, Procedure sp_evaluate_y_yhat, Line 12 [Batch Start Line 0]
Must declare the scalar variable "#data".
The select sum(#data.y) as sum_y, sum(#data.yhat) as sum_yhat line is just to get some output while testing and will be replaced with more operations as soon as I get this to work.
Any idea on what I'm missing?
#data is essentially a table, so you need to treat it as such.
Declare as:
create procedure sp_evaluate_y_yhat
(
#data y_yhat_tabletype readonly
)
as
begin
select sum(y) as sum_y, sum(yhat) as sum_yhat
from #data
end
go
It's worth noting that if #data contained many rows, the row estimate might be off as table variables don't get statistics.

sql error procedure

What's wrong with this code?
CREATE PROCEDURE Proc
(
#factura_id int, #produs_id int, #pret float, #cantitate int,#nr_ordine int
)
as
--declare #factura_id int, #produs_id int, #nr_ordine int, #pret float, #cantitate int
begin
if(((select COUNT (id_produs) from Produse where id_produs=#produs_id)=1))
insert into FacturaProdus(id_factura,id_produs,pret,cantitate,nr_ordine)
values(#factura_id,#produs_id,#pret, CONCAT ('-',convert(float,#cantitate),#nr_ordine))
else
begin
print 'hei'
end
end
I can't find a solution for this.When i execute it, it gives me:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Proc'.
Msg 137, Level 15, State 2, Line 8
Must declare the scalar variable "#produs_id".
Msg 137, Level 15, State 2, Line 9
Must declare the scalar variable "#factura_id".
What to do?
Proc is reserved word in SQL server (I assume you're using it based on syntax and error messages).
So if you really want to create procedure having such a name (I recommend you to choose another name, though) - enclose it into square brackets:
CREATE PROCEDURE [Proc]
(
.....

Setting a varchar column length with a variable or a subquery

I have a table that is created by an import of an Excel file. As is the case, all text fields are imported as nvarchar(255). I'd rather do the bulk import then manipulate the table afterwards. I know SSIS allows me to set data types and sizes via data mapping, but that doesn't seem to work consistently for me. Maybe I'm not holding my mouth right...
Anyway, I want to change the varchar length definitions to the max length of the data in each column. Rather than run a statement to check the max length as a literal...
select max(len(rtrim(FIELD))) from TABLE$
...I want to do it in code. So I hit on this idea:
declare #Var int
set #Var = (select max(len(rtrim(FIELD))) from TABLE$)
alter table dbo.TABLE$ alter column FIELD varchar(#Var)
Lines one and two work fine, but it gives me an error when executing the third line:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '#Var'.
So I tried this, thinking it would be a more compact solution...
alter table dbo.TABLE$
alter column FIELD varchar(select max(len(rtrim(FIELD))) from TABLE$)
...but it wasn't. I got these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
So my question is kind of a two-parter. First, why won't these methods work, and second, what would work--short of finding the mas length of each column, then setting the varchar length with a literal?
Thanks in advance.
Dynamic SQL would work for what you're trying to do. Whether it's the right tool for the job or the right thing to do is another story. Give this a shot:
DECLARE #cmd NVARCHAR(4000)
SET #cmd = 'ALTER TABLE dbo.Table$ ALTER COLUMN FIELD VARCHAR(' + CAST((SELECT MAX(LEN(RTRIM(FIELD))) FROM dbo.TABLE$) AS NVARCHAR(10)) + ')'
EXEC(#cmd)
That query parses but I didn't try to run it. Be careful using Dynamic SQL though. It can get you into trouble if you start using it everywhere. Further reading:
http://sqlmag.com/database-performance-tuning/don-t-fear-dynamic-sql
http://www.sommarskog.se/dynamic_sql.html

Can we call function inside an function SQL Server 2005

ALTER function [dbo].[getEmployeeID](#ID int) returns table
as
begin
return (
select * from [dbo].[gtEmployeeName](2)
select * from Employees where EmployeeID = #ID)
end
here [dbo].[gtEmployeeName] is an other function that I am trying to call.
I am getting an error, can we call or is there any syntax problem?
Msg 156, Level 15, State 1, Procedure getEmployeeID, Line 6
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Procedure getEmployeeID, Line 6
Incorrect syntax near ')'.
Thanks
Prince
If [dbo].[gtEmployeeName] returns scalar you probably are looking for
ALTER function [dbo].[getEmployeeID](#ID int) returns table
as
begin
return (
select *, [dbo].[gtEmployeeName](2) as EmpName from Employees where EmployeeID=#ID)
end
If [dbo].[gtEmployeeName] returns table you probably are looking for
ALTER function [dbo].[getEmployeeID](#ID int) returns table
as
begin
return (
select * from [dbo].[gtEmployeeName](2) EN
inner join Employees E on EN.EmployeeID = E.EmployeeID
where EmployeeID=#ID)
end
Update the join to outer if that is what you need. Also update the join condition (the example assumes that the returned table from gtEmployeeName has a column EmployeeID and that can be used for joining to Employees.
Yes you can call a function inside a function.
In fact, you can call the current function inside the function, to cause a loop.
What error are you getting? Your error is most likely related to something else

Error Handling in T-SQL Scalar Function

This question could easily take multiple paths, so I will hit the more specific path first. While working with SQL Server 2005, I'm trying to create a scalar function that acts as a 'TryCast' from varchar to int. Where I encounter a problem is when I add a TRY block in the function;
CREATE FUNCTION u_TryCastInt
(
#Value as VARCHAR(MAX)
)
RETURNS Int
AS
BEGIN
DECLARE #Output AS Int
BEGIN TRY
SET #Output = CONVERT(Int, #Value)
END TRY
BEGIN CATCH
SET #Output = 0
END CATCH
RETURN #Output
END
Turns out theres all sorts of things wrong with this statement including "Invalid use of side-effecting or time-dependent operator in 'BEGIN TRY' within a function" and "Invalid use of side-effecting or time-dependent operator in 'END TRY' within a function". I can't seem to find any examples of using try statements within a scalar function, which got me thinking, is error handling in a function is possible?
The goal here is to make a robust version of the Convert or Cast functions to allow a SELECT statement carry through depsite conversion errors. For example, take the following;
CREATE TABLE tblTest
(
f1 VARCHAR(50)
)
GO
INSERT INTO tblTest(f1) VALUES('1')
INSERT INTO tblTest(f1) VALUES('2')
INSERT INTO tblTest(f1) VALUES('3')
INSERT INTO tblTest(f1) VALUES('f')
INSERT INTO tblTest(f1) VALUES('5')
INSERT INTO tblTest(f1) VALUES('1.1')
SELECT CONVERT(int,f1) AS f1_num FROM tblTest
DROP TABLE tblTest
It never reaches point of dropping the table because the execution gets hung on trying to convert 'f' to an integer. I want to be able to do something like this;
SELECT u_TryCastInt(f1) AS f1_num FROM tblTest
fi_num
__________
1
2
3
0
5
0
Any thoughts on this? Is there anything that exists that handles this? Also, I would like to try and expand the conversation to support SQL Server 2000 since Try blocks are not an option in that scenario.
Check if you can convert to int first, check out the IsInteger function here: IsNumeric, IsInt, IsNumber It will work on 2000 and up
And, to answer in general: No, you can't use try-catch logic in a function. I can sort of see why - or at least it's clearly preferable to avoid it, given the huge performance penalty that would come with it.
However, I think it is odd that one also cannot RAISE an error in a function... that's something built-in functions already do. I suppose one has to get by returning NULL.
The TRY…CATCH construct cannot be used in a user-defined function in SQL 2012!
See this:
http://msdn.microsoft.com/en-us/library/ms175976.aspx
When I try to use this script:
CREATE FUNCTION u_TryCastInt
(
#Value as VARCHAR(MAX)
)
RETURNS Int
AS
BEGIN
DECLARE #Output AS Int
BEGIN TRY
SET #Output = CONVERT(Int, #Value)
END TRY
BEGIN CATCH
SET #Output = 0
END CATCH
RETURN #Output
END
I got error:
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 10
Invalid use of a side-effecting operator 'BEGIN TRY' within a function.
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 12
Invalid use of a side-effecting operator 'END TRY' within a function.
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 13
Invalid use of a side-effecting operator 'BEGIN CATCH' within a function.
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 15
Invalid use of a side-effecting operator 'END CATCH' within a function.