I want to stop executing on any error occured with out using "try catch",why some errors stop executing while others dont do Examples
Example 1:
select 5;
exec( 'select * from tttt') ; -- tttt is not table
select 6
I can sett in result
5 and 6 and Error Msg Msg 208, Level 16, State 1, Line 1 Invalid object name 'tttt'
in this case I want to skip out
Example 2:
select 5;
update t1 set ID=1 ;--ID is Primary Key
select 6
the result is only Error Msg
Msg 8102, Level 16, State 1, Line 3,Cannot update identity column 'ID'.
I know Example2 Error is on compilation time and Example1 Error is on executing time, But this is just example what I want.
for Example 1 I want the result to be 5 , Error Msg Msg 208 without continuing exeuting to return 6, All that without using try catch or raiseerror or comapring ##ERROR=0
Related
I've got an array of dates in a field called from. It can look something like this.
['2016-05-01', '2016-05-03', '2016-05-04']
I want to SELECT the last item (here 2016-05-04).
I've tried this:
SELECT `from`->"$[JSON_LENGTH(`from`) - 1]" FROM `table` WHERE `id` = 3;
but got that error:
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 2.
I've tried using a variable like this :
SET #count = (SELECT JSON_LENGTH(`from`) - 1 FROM `table` WHERE `id` = 3);
SELECT `from`->"$[#count]" FROM `table` WHERE `id` = 3;
but got the exact same error. But if I do:
SELECT `from`->"$[2]" FROM `table` WHERE `idx` = 3;
It works fine.
you can use :
SELECT JSON_EXTRACT(`from`,CONCAT("$[",JSON_LENGTH(`from`)-1,"]")) FROM `table`;
to get the last item in a json array.
MySQL 8 brings a very straight forward way to accomplish this:
select json_extract(json_array(1, 2, 3, 4, 5), '$[last]');
which returns
5
Can also do cool ranges, like everything but the last one:
select json_extract(json_array(1, 2, 3, 4, 5), '$[0 to last-1]');
which returns
[1, 2, 3, 4]
When I execute this query...
UPDATE tbl a,
(SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl) b
SET tbl.`url` = b.`url`
...I see this error:
Error Code: 1525
Incorrect XML value: 'parse error at line 57 pos 195: '</div>' unexpected (END-OF-INPUT wanted)'
But when I execute this query...
SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl
...the query succeeds.
Why does the UPDATE query fail where the standalone SELECT query succeeds?
I am trying to update the Sphinx Search Realtime Index , using values from my Mysql Table.,
I want to add the new value in the old value of the RT index's record like this ,
i want to achieve this
UPDATE RT_index SET col1 = old_val + new_val WHERE id = xx ;
query i am trying is
UPDATE RT_index SET comments_count = comments_count + 3 WHERE id = 1157642
but Sphinx giving me errror
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT,
expecting CONST_INT (or 4 other tokens) near 'comments_count + 3 WHERE
id = 1157642'
i have tried query like this
UPDATE RT_index SET comments_count = value(comments_count) + 3 WHERE id = 1157642;
but still sphinx gives error,
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT,
expecting CONST_INT (or 4 other tokens) near 'value(comments_count) +
3 WHERE id = 1157642'
How can i add new value in old value using update in sphinx real-time index ?
i am using PHP to do this.
there is not much info about it in http://sphinxsearch.com/docs/current.html#sphinxql-update
You can't. Need to first run a SELECT query to get the current value, and then run the UPDATE.
Not sure if can use a transaction to make the update atomic.
I'm fairly new to SQL and have been stuck trying to run the following code.
SELECT * FROM [User] WHERE LEN([User].[Telephone]) == 10
Errors -
an expression of non-boolean type specified in a context where a condition is expected,
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='.
Any help would be greatly appreciated.
You have to use = insetad of ==
SELECT * FROM [User] WHERE LEN([User].[Telephone]) = 10
becouse = is correct sql equal operator.
I have a procedure named dbo.CLRSPTest which i have encrypted. When I execute the procedure it gets executed but throws an error when I use sp_helptext CLRSPTest to view the code i.e Msg 15197, Level 16, State 1, Procedure sp_helptext, Line 107
There is no text for object 'CLRSPTest'.
Can anyone please help me out??? I am getting puzzled.
This error is raised by the following code in sp_helptext
if (select count(*) from syscomments c, sysobjects o where o.xtype not in ('S', 'U')
and o.id = c.id and o.id = #objid) = 0
begin
raiserror(15197,-1,-1,#objname) b
return (1)
end
This simply means that any object (not a table or system object) which does not have a line in syscomments will return this error.
Encrypted objects have a record in the syscomments table with NULL in the xtext field so they don't get caught by the earlier code. The message you get for those object comes from this query.
if (select count(*) from syscomments where id = #objid and encrypted = 0) = 0
begin
raiserror(15471,-1,-1,#objname)
return (0)
end
Now why do we get an error from the first one and no error for the second one... That can be explained by checking the data in master..sysmessages.
select error, severity, description
from master..sysmessages
where error in (15197, 15471)
and msglangid = 1033
This query returns:
error severity description
15197 16 There is no text for object '%s'.
15471 10 The text for object '%ls' is encrypted.
Here we see that error 15197 has severity 16 and error 15471 has severity 10. On msdn it is explained that error levels 0-9 are not "raised" and that error level 10 gets converted to error level 0 for compatibility reasons.
So to conclude it all. You get this error message because your procedure is a SQL CLR procedure (which don't get any records in the syscomments table)