I need help in converting my Oracle trigger to T-SQL. Any help is greatly appreciated
CREATE TRIGGER "REQUESTOR_TRG"
BEFORE INSERT ON REQUESTOR
FOR EACH ROW
BEGIN
<<COLUMN_SEQUENCES>>
BEGIN
IF INSERTING AND :NEW.ID IS NULL THEN
SELECT REQUESTOR_SEQ.NEXTVAL INTO :NEW.ID FROM SYS.DUAL;
END IF;
END COLUMN_SEQUENCES;
END;
I am getting these errors:
Msg 102, Level 15, State 1, Procedure REQUESTOR_TRG, Line 2
Incorrect syntax near 'BEFORE'
Msg 4145, Level 15, State 1, Procedure REQUESTOR_TRG, Line 7
An expression of non-boolean type specified in a context where a condition is expected, near 'AND'.
Msg 102, Level 15, State 1, Procedure REQUESTOR_TRG, Line 8
Incorrect syntax near ':'.
Msg 102, Level 15, State 1, Procedure REQUESTOR_TRG, Line 10
Incorrect syntax near 'COLUMN_SEQUENCES'.
Related
I use a case when it works perfectly fine on MySQL but not MS SQL please help.
It seems the equal is not accepted -- if not what then will work
SELECT A FROM TABLE A
WHERE
CASE WHEN COUNT = 2 THEN GOAL = 2 ELSE GOAL = 3 END
Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword
'CASE'.
You need to change your query to WHERE columnName = value.
The following query is what you are expecting:
SELECT A.*
FROM TABLE A
WHERE GOAL = CASE WHEN COUNT = 2 THEN 2 ELSE 3 END
select 2678400 * 1393
When I run this query, I'm getting this error in SQL Server:
Msg 8115, Level 16, State 2, Line 23
Arithmetic overflow error converting expression to data type int.
Please help me to resolve this issue.
Since this result overflows the capacity of INT - use BIGINT instead:
SELECT CAST(2678400 AS BIGINT) * 1393
This question already has answers here:
Insert error, Incorrect syntax near '2' [closed]
(2 answers)
Closed 8 years ago.
This is my INSERT statement:
INSERT INTO Exercise 2 (Exercise, [Calories Burned])
VALUES ('Swimming', 500)
This is the error message that I receive:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '2'.
You have an invalid table name... you need to put brackets around it if there is a a space. Example: [Exercise 2]
Try this
INSERT INTO [Exercise 2] (Exercise, [Calories Burned]) VALUES ('Swimming', 500)
I'm using bulk insert to read a text file into a SQL Server table. I keep getting the same error when I try using bulk insert, even though if I take the first line of the text file, and copy the values into a plain old insert into statement, everything works. My database, stop_times, is setup as follows:
train_num int,
arrival_time time,
dept_time time,
station_name varchar(50),
seq_num, int
My .txt file has the following format
101,'04:30:00','04:30:00','San Jose',1
101,'04:35:00','04:35:00','Santa Clara',2
101,'04:40:00','04:40:00','Lawrence',3
Running
bulk insert dbo.stop_times
from 'C:\Users\amanda\Desktop\stop_times1.txt'
with(
FIELDTERMINATOR=','
)
Gives me
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (train_num).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 2 (arrival_time).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 2 (arrival_time).
etc
However, copying and pasting the first row into
insert into stop_times(train_num, arrival_time, dept_time, station_name, seq_num)
values (101, '04:30:00', '04:30:00', 'San Jose', 1);
works. I feel like I'm missing something totally obvious in the bulk insert function, but am clueless.
try this
bulk insert dbo.stop_times
from 'C:\Users\amanda\Desktop\stop_times1.txt'
with(
FIELDTERMINATOR=',',
ROWTERMINATOR = '\n'
)
IF you have column names in 1st row u need to do something like this
bulk insert dbo.stop_times
from 'C:\Users\amanda\Desktop\stop_times1.txt'
with(
FIELDTERMINATOR=',',
ROWTERMINATOR = '\n',
FIRSTROW = 2
)
I'm attempting to convert a MySQL query to a T-SQL query and the IF statement that's enclosed within a SUM statement is tripping me up. Any suggestions?
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(if(CMTS_RQ.US_Pwr>=37 and CMTS_RQ.US_Pwr<=49)) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
But I get an error:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'if'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
T-SQL doesn't have a "inline" IF statement - use a CASE instead:
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(CASE
WHEN CMTS_RQ.US_Pwr >=37 AND CMTS_RQ.US_Pwr <= 49
THEN 1
ELSE 0
END) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
So if the value of CMTS_RQ.US_Pwr is >= 37 AND <= 49 then add 1 to the SUM - otherwise 0. Does that give you what you're looking for?
In SQL Server 2012 and newer, you can use the new IIF function:
SUM(IIF(CMTS_RQ.US_Pwr >= 37 AND CMTS_RQ.US_Pwr <= 49, 1, 0)) AS us_pwr_good