Rerun Error Message While Creating a Function - sql

i am fairly new to SQL programming, and I am currently learning to create FUNCTIONS.
The problem that I am having, is creating the following FUNCTION.
create function CreatePI
(
)
returns decimal(10,6)
with returns null on null input
as
begin
declare #P as decimal(10,6)
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)
return #P
end
go
The above function is supposed to replicate the number PI. But the problem that I am having is:
Msg 156, Level 15, State 1, Procedure CreatePI, Line 11
Incorrect syntax near the keyword 'return'.
If anyone could help me out with why I am getting this problem, it would be greatly appriciatead.

You are missing a closing paren on the set line:
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15))
----------------------------------------------------------^

Related

How to declare and set a value in SQL?

I am trying to simply declare and set variables in SQL.
I am still learning and using and online platform such as this one https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_no_distinct
I am simply doing this:
DECLARE #Var1 INT;
SET #Var1 = 1;
And I already have an error message for the first line
Error: near line 1: near "DECLARE": syntax error Error: near line 17: near "SET": syntax error
What am I missing here?
Thank you

Msg 8115, Level 16, State 6, Line 4 Arithmetic overflow error converting varchar to data type numeric

This is the query I have written.
DECLARE #SQL_BULK VARCHAR(MAX)
declare #cp decimal(14,2)=1000
declare #tb varchar(20)='tbl_LT'
set #SQL_BULK='insert into '+#tb+'(ClosePrice) values('''+#cp+''');'
EXEC (#SQL_BULK)
When I execute the query I am getting Msg 8115, Level 16, State 6, Line 4
Arithmetic overflow error converting varchar to data type numeric. as error.
I have tried cast, conversion methods.
The + operator is overloaded in SQL Server. If any argument is numeric, then it is a string.
Often, I do what you want to do using replace() to prevent this problem:
set #SQL_BULK = 'insert into #tb(ClosePrice) values(#cp)';
set #SQL_BULK = replace(#SQL_BULK, '#tb', #tb);
set #SQL_BULK = replace(#SQL_BULK, '#cp', #cb);
EXEC (#SQL_BULK)
As a note: you should probably use `sp_executesql and pass in the second value as a parameter:
set #SQL_BULK = 'insert into #tb(ClosePrice) values(''#cp'')';
set #SQL_BULK = replace(#SQL_BULK, '#tb', #tb);
exec sp_executesql #SQL_BULK, N'#cb decimal(14, 2)', #cp = #cp;
Whenever you add Numerics to a string, the default behavior is to convert the string to Number.
In this case
set #SQL_BULK='insert into '+#tb+'(ClosePrice) values('''+#cp+''');'
you are adding #cp to a string which is causing the problem.
You have to re-write this as
set #SQL_BULK='insert into '+#tb+'(ClosePrice) values('''+CAST(#cp AS VARCHAR)+''');'

How do I execute a function that returns a table and accepts a NVARCHAR

I have a function that I would like to execute to see what it does:
ALTER FUNCTION [dbo].[DOCUVALUEUSERFILTERED] (#WINDOWSID NVARCHAR(255))
RETURNS TABLE AS
RETURN
SELECT *
FROM DOCUVALUEWITHUSERIDS
WHERE WINDOWSID = #WINDOWSID
I tried
Select *
from [DOCUVALUEUSERFILTERED]('rigamonk')
but I get errors:
Msg 313, Level 16, State 2, Line 1
An insufficient number of arguments were supplied for the procedure or function DBO.FIELDMASK.
Msg 4413, Level 16, State 1, Line 1
Could not use view or function 'DOCUVALUEUSERFILTERED' because of binding errors.
I would bet that DOCUVALUEWITHUSERIDS is a function as well and that it has required parameters.
try running just the select outside of the function
select top 1 * from DOCUVALUEWITHUSERIDS
If you get an error then you need to know more about how DOCUVALUEWITHUSERIDS works.

getting error for if/else in a stored procedure

I am relatively very new to SQL queries but I have this stored procedure where I am trying to get value of a declared variable as mentioned below but getting error,
First line is 20 here,
declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if (select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_variable = 0
Exception I get:
Msg 4145, Level 15, State 1, Procedure GetID, Line 20
An expression of non-boolean type specified in a context where a
condition is expected, near 'set'. Msg 156, Level 15, State 1,
Procedure GetID, Line 21 Incorrect syntax near the keyword 'else'.
Now if I remove declare #g... and try to parse it, no error occurs
EDIT
I want my code to check for returned value by my select statement so "if exists" is not really what am looking for, sorry.
try use if exists:
declare #g_v bit
if exists(select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_v = 0
declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if ((select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable) = value )
set #g_v = 1
else
set #g_variable = 0
You can't say
if (select ...
You have to compare something with something else in a if statement, or use a boolean function such as exists

Calling a custom SQL function

This feels like a basic question, but I have tried everything! I'm writing a custom function SQL function to convert integers into dates:
CREATE FUNCTION convert_to_date (#fin INT)
RETURNS DATE
AS
BEGIN
DECLARE #fout DATE
SET #fout = CASE WHEN #fin IN ('','99999999','0','1') THEN NULL
ELSE CONVERT(DATE,CAST(#fin AS CHAR(8)))
END
RETURN #fout
END
SELECT dbo.convert_to_date(DtSurgDischarge) AS DischargeDate
FROM [TR_MASTER.registry].[dbo].[mgh_tumor]
I get the following error message:
Msg 156, Level 15, State 1, Procedure convert_to_date, Line 16
Incorrect syntax near the keyword 'SELECT'.
What am I doing wrong? Feels like it must be obvious. Thanks everyone!
At the very least you need a GO after the END statement of the function, if you're attempting to execute both the function create and the SELECT in one script - assuming you're using SSMS to run the script, that is.
CREATE FUNCTION convert_to_date (#fin INT)
RETURNS DATE
AS
BEGIN
DECLARE #fout DATE
SET #fout = CASE WHEN #fin IN ('','99999999','0','1') THEN NULL
ELSE CONVERT(DATE,CAST(#fin AS CHAR(8)))
END
RETURN #fout
END
GO
SELECT dbo.convert_to_date(DtSurgDischarge) AS DischargeDate
FROM [TR_MASTER.registry].[dbo].[mgh_tumor]