Incorrect syntax near 'RAISEERROR' [closed] - sql-server

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
Using the following query to change a stored procedure in SQL Server 2008 RC2:
ALTER PROCEDURE SetPackageCurrentVersion
(#PackName VARCHAR(32),
#PackVersion VARCHAR(32))
AS
BEGIN
SET NOCOUNT ON;
UPDATE Packages
SET CurrentPackageVersionId = (SELECT pv.id
FROM PackageVersions pv
JOIN Packages p ON p.Id = pv.Package_Id
WHERE p.Name = #PackName
AND pv.Name LIKE '%' + #PackVersion + '_Tag'),
UpdatedAt = CURRENT_TIMESTAMP
WHERE Name = #PackName;
IF ##ROWCOUNT < 1
RAISEERROR('No row updated for package %s', 11, 1, #PackName)
ELSE IF ##ROWCOUNT > 1
RAISEERROR('More than one row updated for package %s', 11, 1, #PackName)
END;
However, I get the following unhelpful error:
SQL Error [102] [S0001]: Incorrect syntax near 'RAISEERROR'.
Incorrect syntax near 'RAISEERROR'.
I tried changing the RAISEERROR to a THROW but then realized that this was only added to SQL Server 2012. Similarly I tried adding and removing semicolons (I'm never quite sure about when to use them or not in SQL) but to no avail.
What's wrong with my query?

Related

my new created table appears in master(in system databases) instead of my created database in sql server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
my new created table appears in master(in system databases) instead of my created database in sql server.
here's my code:
create table patients(
id int not null ,
fname char(20) not null,
lname char(20) not null,
disease char(30),
primary key(id)
);enter image description here
Because you forget to begin with the
USE MyDatabase
Statement, that wil executes your DDL queries into the right db.

Incorrect syntax of sp_rename [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to rename team view to myteam.
create view team as (select * from employees);
I'm writing the sp_rename command but it, showing error.
sp_rename team myteam;
[12:31:15 pm] Started executing query at Line 685
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'myteam'.
Total execution time: 00:00:00.012
sp_rename syntax:
sp_rename old_name, new_name;
There is a , between old_name and new_name.
Syntax:
sp_rename 'old_table_object', 'new_table_object'
The result outputs of this procedure might be 0 or non-zero values. 0 value indicates that the procedure execution successfully completed and non-zero values indicate failure.
How to rename tables in SQL Server with the sp_rename command

T-SQL insert into doesn't recognize a table name [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The following code doesn't work:
INSERT INTO [dbo].[Customers]
([CustName]
,[CustSurname]
,[City])
VALUES
("Joe", "Dassen", "London")
The table definitely exists, and column names are definitely correct.
However, all object references are red-underlined. Pointing the cursor show this: "invalid object name Customers" for the table name, and "Invalid column name CustName" for [CustName] and so on.
Running the code generates errors, but different ones:
Msg 207, Level 16, State 1, Line 19
Invalid column name 'Joe'.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'Dassen'.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'London'.
I use SQL Server 2017 Express edition and latest SSMS.
It should be as below: You are passing values in double quote it should be in single quote.
INSERT INTO [dbo].[Customers]
([CustName]
,[CustSurname]
,[City])
VALUES
('Joe', 'Dassen', 'London')
Somehow if you want to insert values like Jo'e then you need to pass two times single quote as '' not the double quote. So, it should be Jo''e.
Your query should be like below :
INSERT INTO [dbo].[Customers]
([CustName]
,[CustSurname]
,[City])
VALUES
('Joe', 'Dassen', 'London')
Double quotes are for identifiers but String values must be quoted with single quotes.
So instead of using double quotes, just replace them by single quotes.How do I use single quotes in SQL query?

Creating a Temp table where the rows equal a number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to figure out how I can create a temp table in sql server that will create a number of rows based on a number.
So I have a query that results the number 35 and I want to create a temp table with 35 rows with the ID values 1 - 35.
declare #i int
set #i = 1
while (#i <= 35)
begin
insert tmp (id) values (#i)
set #i = #i + 1
end

Error with select case statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to convert this T-SQL query into Oracle?
if((select case
when (select top 1 AlertMessage
from ims.dbo.alertlist
where SystemID=#meter
order by TimePeriod desc
) like '%NO_COMMUNICATION_Resolved' then 1
else 0 end)=1)
begin
INSERT INTO [ims].[dbo].[alertlist]
([SiteID],[ThresholdNumber],[SystemID],
[AlertMessage],[TimePeriod],[AlertType],[PollID])
VALUES
(1,#thresnumber,#meter,#message,getdate(),1,0)
end
As pointed out by Dan Puzey, your question is too broad and it would require a lot of trial and error. However, I'll try to put you on the right track with something that might solve the first part of your problem.
DECLARE v_IsMessageResolved int;
-- This query will retrieve the value of the first row returned by the sub-query, mimicking the TOP 1 of SQL Server, and it will store the value of MessageResolved in a Variable
SELECT
MessageResolved into v_IsMessageResolved
FROM
(
-- This query will return 1 for all the Messages that match the LIKE clause, ordered by TimePeriod in descending order.
SELECT
CASE
WHEN AlertMessage LIKE '%NO_COMMUNICATION_Resolved' THEN 1
ELSE 0
END AS MessageResolved
,RANK() OVER (ORDER BY TimePeriod DESC) AS MessageRank
FROM
ims.alertlist
WHERE
(SystemID = :meter)
)
WHERE
(MessageRank = 1)
-- At this point, it will be a matter of checking the value of v_IsMessageResolved and, if it's "1", run the INSERT
Please note that I know SQL Server very well, but I never used Oracle, therefore my solution might not be perfect (or even run at all, as I don't have an environment to test it). This also means that you can find the answers to the remaining questions you might have with a simple search, as I did. :)

Resources