Can we call function inside an function SQL Server 2005 - 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

Related

Error when I try to assign a value from a table to a variable in SQL

DECLARE UserIdToUpdate
SELECT UserIdToUpdate = customer_id
FROM dbo.orders
I get this error message (translated from the Spanish version):
Msg 137, Level 15, State 1, Line 13
You must declare the scalar variable '#UserIdToUpdate'
You need first to decalre the user defined varoable before using it
CREATE tABLe orders( customer_id INTEGER)
GO
DECLARE #UserIdToUpdate INTEGER
SELECT TOP 1 #UserIdToUpdate = customer_id FROM dbo.orders
GO
db<>fiddle here

How to affect one selected Row to a local variable in T-SQL

I try to select one row from a table, and affect its value to a local declared variable I used the method below, but I got error.
Method:
DECLARE #A AS INT ;
SET #A=1;
WHILE #A<=10
BEGIN
DECLARE #R AS TABLE
SET #R = (SELECT * FROM Client c WHERE c.ID=#A)
SET #A=#A+1
END
Error I got :
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'SET'.
So please how can I affect the subquery result to the R local variable ?
Table Variables cannot be assigned; you use DML to add data to a table variable. From docs
Assignment operation between table variables isn't supported.
table (Transact-SQL)
So something like:
DECLARE #R AS TABLE(ClientID int, Name nvarchar(200), ...)
INSERT INTO #R(ClientId, Name, ...)
SELECT ClientId, Name, ...
FROM Client c
WHERE c.ID=#A

SQL Create View error 'begin'

I'm getting an error with my BEGIN keyword.
'Msg 156, Level 15, State 1, Procedure AdminReport, Line 3 Incorrect
syntax near the keyword 'BEGIN'
. Also my "customers.firstname' could not be Bound.
CREATE VIEW [dbo].[AdminReport]
AS
BEGIN
SELECT
b.bookingID,
b.totalCost,
b.bookingDate,
b.paymentConfirmation,
c.customersID,
customers.firstname,
c.surname,
c.contactNum,
paymentConfirmation
FROM
booking b
INNER JOIN customers c
ON b.customerID= c.customersID
Where
paymentConfirmation = 'False'
ORDER BY
bookingDate ASC
END
GO
Could someone help please! Thanks.
your customer.firstname cannot be bound because you are renaming the table as "c" so use c.firstname
is paymentconfirmation from b? If so might as well state it on the query to keep it consistent. Run the select statement by itself and see if it gives you an error.
Just remove the BEGIN and END. They're not needed in CREATE VIEW syntax.
See: http://www.w3schools.com/sql/sql_view.asp

Incorrect syntax near the keyword 'if' when inside function, but no error when not in a function

I am running into an issue with a statement in my function, however, when I don't use it in a function it works perfectly fine and I have no idea why...Here is the statement in question..
RETURNS TABLE
AS
RETURN
if (#PreAQ = 0)
begin
select * from [MyTable]
where ParentSysID = #ParentSysID
and (
CodeID like 'AD -%'
or CodeID like 'CP -%'
or CodeID like 'DB%'
or CodeID like 'MC%'
or CodeID like 'SD -%'
or CodeID like 'VA%'
or CodeID like 'WD -%'
)
end
I have multiple if statements, identical with that that applies more filters.
Here is the full error
Msg 156, Level 15, State 1, Procedure WT_FN_GET_MyTable_By_ParentSysID, Line 11
Incorrect syntax near the keyword 'if'.
and I am using SQL Server 2008R2
You cannot have flow control logic (if statement) in an inline (single-statement) function. Take a look at how to declare multi-statement function instead.

Getting an invalid Object error while doing CTE operation in Stored procedure?

I got an error invalid Object SubSkillIds while executing this in a stored procedure? Can anybody let me know why is it so?
My error message looks like:-
Msg 208, Level 16, State 1, Procedure
SubSkillDelete, Line 47
Invalid object name 'SubSkillIds'.
Code:
WITH SubSkillIds (SubSkillId) AS
(
-- Base case
SELECT
SubSkillId
FROM dbo.SubSkill
WHERE RegressionSubSkillId = #SubSkillId
UNION ALL
-- Recursive step
SELECT
S.SubSkillId
FROM dbo.SubSkill S
WHERE S.RegressionSubSkillId IN (
SELECT
SubSkillId
FROM dbo.SubSkill
WHERE RegressionSubSkillId = #SubSkillId)
)
SELECT #SubSkillIdFound = SubSkillId
FROM SubSkill WHERE SubSkillId = #SubSkillId
DELETE FROM SubSkillActivity WHERE SubSkillId = #SubSkillId
DELETE FROM SubSkill WHERE RegressionSubSkillId
IN (SELECT * FROM SubSkillIds)
DELETE FROM SubSkill WHERE SubSkillId = #SubSkillId
The CTE is only available for the next statement - your SELECT......
It is no longer available later on - that's why your second DELETE statement, which references that CTE, will fail.
If you need to keep the values of the CTE around for more than one single statement, you need to store those values into a temporary table / table variable.