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
Related
Basically I want to run a SQL statement like this, but I can't due to errors.
Not sure why?
INSERT INTO allevent (eventname)
VALUES (select username from registered)
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 ')'.
No need of parentheses around the SELECT statement in your query.
INSERT INTO allevent (eventname)
SELECT username FROM registered
Parentheses with VALUES required, where some places like below:
INSERT INTO tablename (fieldname)
VALUES ('field 01'), ('field 02');
Please find the syntax for the INSERT statement.
This is my code:
ALTER PROCEDURE [db_owner].[get_checkin_likes]
#likes integer
AS
Select count(*)
from
(
select checkin_id as Checkin, users_id as 'Liked by'
from checkin_likes
where #likes = checkin_id
)
When I execute this command I expect two things:
I expect to get a list of all the users that like the particular checkin I selected
I expect to get a count of how many users like the particular checkin
However, when I try to run the command it returns this:
Msg 102, Level 15, State 1, Procedure get_checkin_likes, Line 9
Incorrect syntax near ')'.
Any suggetions as to how to fix this or why it's happening?
You need to name your subquery; try putting something after the final parenthesis:
ALTER PROCEDURE [db_owner].[get_checkin_likes]
#likes integer
AS
Select count(*)
from
(
select checkin_id as Checkin, users_id as 'Liked by'
from checkin_likes
where #likes = checkin_id
) a
I probably should mention that when you run this query you will only get the count back. I'm not sure of your use-case here, but you can do an OVER and return everything with a count at the end
EDIT:
I would personally do this and have it all in the one record set:
ALTER PROCEDURE [db_owner].[get_checkin_likes]
#likes integer
AS
select checkin_id as Checkin
, users_id as 'Liked by'
, COUNT(checkin_id) OVER () AS total_rows,
from checkin_likes
where #likes = checkin_id
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.
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
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.