I get this error:
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near '.'.
On this code:
SELECT
dest.text
FROM
sys.dm_exec_query_stats
AS
deqs
CROSS
APPLY
sys.dm_exec_sql_text
(
deqs.sql_handle -- This is line 11.
)
AS
dest
WHERE
deqs.last_execution_time
>
'2018-07-01 00:00:00'
AND
dest.text
LIKE
'%QuittanceAudit%'
;
I spread that code out on multiple lines so that the error message could more precisely report the location of the error.
Or, in a standard format:
SELECT dest.text
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE deqs.last_execution_time > '2018-07-01 00:00:00'
AND dest.text LIKE '%QuittanceAudit%'
;
The problem I am trying to solve is getting the history of the select statements that have run using this technique:
https://dba.stackexchange.com/questions/4043/can-i-see-historical-queries-run-on-a-sql-server-database
It seems to me there is nothing wrong in your syntax, it runs fine on my server.
Are you using a user account different from sa?
To run dm_exec_sql_text(deqs.sql_handle) you need VIEW SERVER STATE permission on the server and to run dm_exec_query_stats you need both VIEW SERVER STATE permission on the server and VIEW DATABASE STATE permission in the database.
Try this:
GRANT VIEW SERVER STATE TO YourUserAccount
GRANT VIEW DATABASE STATE TO YourUserAccount
Related
I was migrating an old app to a new hosting got a super weird error that looks like this.
This throws error in PHP MySQLi (Unknown column 't0.colx' in 'order clause'):
SELECT *
FROM (
SELECT colx, ...
FROM ...
) AS t0
ORDER BY t0.colx
This passes without problem:
SELECT *
FROM (
SELECT colx, ...
FROM ...
) AS t0
ORDER BY colx
Removing the t0 shows no error whatsoever.
In the old server I was using both works.
Anyone can point me out if I'm doing something wrong?
EDIT:
just to clarify, the error in being thrown by PHP's Mysqli not Mysql iself I think, if I copy paste the sql into phpmyadmin it throws no error and returns the data correctly. The error message I'm getting from mysqli object in mysqli->error property after I check if the mysqli->execute() returns false.
Versions:
PHP Version 5.5.38
Mysql 5.7.39
Removing "AS" didnt work.
The column named for "ORDER BY" its selected this way, maybe thats throwing the error? but on older hosting no error is shown.
SELECT
*
FROM
(
SELECT
...
if(numDoc IS NOT NULL, numDoc, CAST(numDocEx AS UNSIGNED)) AS numDoc,
...
) t0
ORDER BY t0.numDoc
Trying to create a simple view which counts some data and brings it up, but MySQL is throwing this error:
Error Code: 1234. Incorrect usage/placement of 'SQL_BUFFER_RESULT'
Code:
CREATE or replace
ALGORITHM = UNDEFINED
DEFINER = `root`#`%`
SQL SECURITY DEFINER
VIEW `view_test` AS
SELECT
a.uf AS uf,
CAST(a.empresas_total AS UNSIGNED) AS total_21,
CAST(b.empresas_total AS UNSIGNED) AS total_20,
(select CAST(SUM(a.empresas_total - b.empresas_total) AS DECIMAL)) AS DIF_total_2020
FROM
base.tableA a
left join base.tableB b
on
a.uf=b.uf
GROUP BY a.uf;
This is not working.
When delete the line
(select CAST(SUM(a.empresas_total - b.empresas_total) AS DECIMAL)) AS DIF_total_2020
it works fine.
The view is created and fully functional.
Can anyone spot my error?
Actually, by doing the select itself, it goes OK. it only ends in a error when creating the view.
Found out what is going on:
The line
(select CAST(SUM(
is the problem, obviously. But the problem is that when creating the view, somehow the server miss the SQL_BUFFER_RESULT in there, as it create the view automatically with the SQL_BUFFER_RESULT in the first line of the select, and it is valid for EVERY field described after that.
The solution was to rewrite the line by removing the select and making it sum without it.
SUM((CAST(a.empresas_total AS DECIMAL))-(CAST(b.empresas_total AS DECIMAL))) AS DIF_empresas_total
So, the view was created using a single statement of SQL_BUFFER_RESULT and worked:
CREATE or replace
ALGORITHM = UNDEFINED
DEFINER = `root`#`%`
SQL SECURITY DEFINER
VIEW `view_test` AS
SELECT SQL_BUFFER_RESULT
a.uf AS uf,
CAST(a.empresas_total AS UNSIGNED) AS total_21,
CAST(b.empresas_total AS UNSIGNED) AS total_20,
SUM((CAST(a.empresas_total AS DECIMAL))-(CAST(b.empresas_total AS DECIMAL))) AS DIF_empresas_total
FROM
base.tableA a
left join base.tableB b
on
a.uf=b.uf
GROUP BY a.uf;
Worked fine!
Thanks.
I have installed Orbeon Forms 2018.1.1 CE using Mysql as a relational database (Mysql version: 8.0.13).
Although I am able to create, save and publish any form, when I try to access the summary of a form, I get an 'Error performing search' error. The same error is also displayed every time I try to search for a form on the summary page using the 'Search Forms' control on the upper section of the page.
In addition to that, the summary page is always empty, although the relevant table in the database contains the appropriate records. The application is running on the Apache Tomcat (8.5.35) and the database was created using the mysql-2017_2.sql script.
I have also checked the orbeon.log file and I've found the following entry:
An Error has Occurred
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to
use near row_number FROM orbeon_i_current c, (select #rownum := 0) r at line 12
Application Call Stack
oxf:/apps/fr/page-flow.xml
reading page view data output 46
element=<service path="/fr/service/(oracle|mysql|postgresql|db2|sqlserver)/search/([^/^.]+)/([^/^.]+)" view="persisten view =persistence/relational/search.xpl
oxf:/apps/fr/persistence/relational/search.xpl
reading processor output 22
element=<p:output name="data" ref="data"/>
name =data
ref =data
I am wondering if there is any other required configuration for the summary page to work properly.
Using the P6Spy application, I've realized that the following query seems to be responsible for the error message:
SELECT count(*)
FROM (
SELECT c.data_id,
c.document_id,
c.draft,
c.created,
c.last_modified_time,
c.last_modified_by,
c.username,
c.groupname,
c.organization_id,
#rownum := #rownum + 1 row_number
FROM orbeon_i_current c
, (select #rownum := 0) r
WHERE c.app = 'DemoApp' AND
c.form = 'DemoForm'
AND c.draft = 'N'
) a
Actually, the problem is probably in the line:
#rownum := #rownum + 1 row_number
MySQL introduced the window function ROW_NUMBER() since version 8.0 and in this line the literal 'row_number' is translated by the sql engine as a function.
This is a bug, and it is fixed since Orbeon Forms 2018.2.1 PE.
On my test SQL Server 2014 installation, I was "cleaning" the master database.
With the following command, I was checking which user objects there are:
SELECT
'DROP ' +
CASE
WHEN [sys].[all_objects].type IN ('AF','FN','FS','FT','IF','TF') THEN 'FUNCTION '
WHEN [sys].[all_objects].type IN ('D','C','F','PK','UQ') THEN 'CONSTRAINT '
WHEN [sys].[all_objects].type IN ('IT','S','U') THEN 'TABLE '
WHEN [sys].[all_objects].type IN ('P','PC','RF','X') THEN 'PROCEDURE '
WHEN [sys].[all_objects].type IN ('TA','TR') THEN 'TRIGGER '
WHEN [sys].[all_objects].type = 'R' THEN 'RULE '
WHEN [sys].[all_objects].type = 'SN' THEN 'SYNONYM '
WHEN [sys].[all_objects].type = 'TT' THEN 'TYPE '
WHEN [sys].[all_objects].type = 'V' THEN 'VIEW '
END +
SCHEMA_NAME(sys.[all_objects].[schema_id]) + '.' + OBJECT_NAME(object_id) + '; ' as [Command],
OBJECT_NAME(object_id) as [ObjectName],
[sys].[all_objects].[type_desc] as [TypeDesc],
[sys].[all_objects].[type] as [Type],
SCHEMA_NAME(sys.[all_objects].[schema_id]) as [Schema]
FROM
sys.[all_objects] WITH (NOLOCK)
WHERE SCHEMA_NAME(sys.[all_objects].[schema_id]) like '%dbo%'
One of the results was the view spt_values.
Command | ObjectName | TypeDesc | Type | Schema
------------------------|------------|----_-----|------|-------
DROP VIEW dbo.spt_values; spt_values VIEW V dbo
As it was not one of the views I knew, I deleted it (along with other objects).
Later that day, I wanted to check the properties of a database in SSMS 2016 and got the following error:
After some searching, I found that I could recreate the missing view with the script u_tables.sql (which is in the SQL Server installation folder on your server). Information from here: https://ashishgilhotra.wordpress.com/tag/u_tables-sql/
The code in that script to create the view is the following:
create view spt_values as
select name collate database_default as name,
number,
type collate database_default as type,
low, high, status
from sys.spt_values
go
EXEC sp_MS_marksystemobject 'spt_values'
go
grant select on spt_values to public
go
Already when looking at the code, I doubted that it would work, as there is no sys.spt_values table anywhere to be found.
As expected I get the error
Msg 208, Level 16, State 1, Procedure spt_values, Line 6
Invalid object name 'sys.spt_values'.
On my other server with SQL Server 2008 on it, there is a table master.dbo.spt_values (but no view)!
After some more searching, I found that I could just create a table with the same name.. Link here https://www.mssqltips.com/sqlservertip/3694/fix-invalid-object-name-masterdbosptvalues-when-viewing-sql-server-database-properties/
Now I create a table with the values from another SQL Server 2014 installation, and everything seems to be working again.
But, it is not correct!
When I check the new created object on the test server with this command
select [name] , [type], [type_desc]
from sys.objects
where name like 'spt_v%'
It shows a user_table object. On my other server, it shows a view...
So, my question is: How can I create the view spt_values which gets its data from a table spt_values?
Ok, after some fiddling arround, I found the solution..
The table sys.spt_values is in the ressources database (mssqlsystemresource). This database is only accessible when the SQL Service is started in single user mode..
To re-create the view I had to do the following steps:
Stop all SQL Services
2. Start the SQL Service in single user mode
Open a DOS Command prompt and start the sqlservice with the switch -m
sqlservr.exe -sSQLT01 –m
Connect SSMS to the instance
Just connect the query window, but not the Object Explorer window. The service only accepts one single connection! If there is a problem, you can see it in the DOS Window where the service is running.
Delete the wrong table spt_values
As I created a table spt_values on the master database, I have to delete it first
use master
go
drop table dbo.spt_values
5. Create the view
Now I finally can create the view dbo.spt_values, which points to the table sys.spt_values
use master
go
create view spt_values as
select name collate database_default as name,
number,
type collate database_default as type,
low, high, status
from sys.spt_values
go
EXEC sp_MS_marksystemobject 'spt_values'
go
grant select on spt_values to public
go
6. Check the dbo.spt_values object
use master
select schema_name(schema_id), object_id('spt_values'), *
from sys.objects
where name like 'spt_v%'
It should show a view now
Query the view dbo.spt_values and the table sys.spt_values
Just for the fun of it... You can now query the table sys.spt_values, which is in the ressources database
use mssqlsystemresource
Select * from sys.spt_values
And you can query the view dbo.spt_values, which is in the master database
use master
Select * from dbo.spt_values
8. Restart the services
You can now quit the DOS window with the SQL Service running and start the SQL Services. Or you just restart the whole server
Hope this post will help others in the future
The u_tables.sql script will create master.dbo.spt_values, but I had to run it with the Dedicated Administrator Connection (DAC).
Solution:
Step 1: Open the Command Prompt as Admin
Step 2: Run the following:
sqlcmd -S -U sa -P -A -i "C:\Program Files\Microsoft SQL Server\MSSQL14.SQL2017\MSSQL\Install\u_tables.sql"
Swap out and with your values. If you don't have the password to sa you will need to find a user with sufficient rights (and replace 'sa' with your user name).
The -A runs the command as the DAC. This should be used sparingly. See MS documentation on the DAC.
Find the u_tables.sql file in your installation directory. The path above is where it is on my machine with SQL 2017 installed in the default location on the C: drive.
I have a CRON job which executes a SELECT statement to grab records. When the SELECT runs on my dev machine, it produces the following statement:
SELECT `users`.* FROM `users` WHERE `users`.`id` = 87 LIMIT 1
This is successful.
When the SELECT runs on my production (hosted) machine it produces the statement with double quotes:
SELECT "users".* FROM "users" WHERE "users”.”id” = 87 LIMIT 1
This is not successful and I get a MySQL 1064 error,
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.* FROM "users" WHERE "users
The code is the same on both machines, but my dev MySQL is version 5.5.33, whereas production is 5.1.67 (I don't have control over this to set/update it)
Is there a way to force single quotes or another preferred method to handle this situation?
Thanks for your time and assistance.
--EDIT--
Here are the main code snippets that are invoked via my CRON job:
/lib/tasks/reports.rake
namespace :report do
desc "Send Daily Report"
task :daily => :environment do
User.where(:report_daily => 1).find_each do |user|
ReportsMailer.send_report(user, 'daily').deliver
end
end
/app/mailers/reports_mailer.rb
def send_report(user, date_increment)
#user = user
#date_increment = date_increment
get_times(user)
mail :to => user.email, :subject=> "Report: #{#dates}"
end
--EDIT2--
So it looks like I need to use slanted single quotes (`) in order for this to work successfully. How do I force my app or MySQL to use these instead of double (") quotes?
I don't know why it does this, but I do know that if you're referencing column names in MYSQL, you need to use ``, whereas values / data should be wrapped in "", like this:
SELECT `users`.* FROM `users` WHERE `users`.`id` = "87" LIMIT 1
I learnt this the hard way back in the day when I was learning how to do simple MYSQL queries
Here's some documentation from MYSQL's site for you:
The identifier quote character is the backtick (“`”):
mysql> SELECT * FROM `select` WHERE `select`.id > 100;
Identifier quote characters can be included within an identifier if
you quote the identifier. If the character to be included within the
identifier is the same as that used to quote the identifier itself,
then you need to double the character. The following statement creates
a table named a`b that contains a column named c"d:
mysql> CREATE TABLE `a``b` (`c"d` INT);
Is there any reason you couldn't put some of your sql statement directly into your code like:
User.where("`report_daily`=1").find_each do |user|
After further inspection, and working with my hosting company, its turns out that my query is timing out on their server. Thanks to all that responded.
Since you are not using any literals, the format of the generated SQL statements should be determined by the underlying adapter. Perhaps you have a different mysql adapter installed or configured on each machine. Check the installed version. For example:
bundle show mysql
and also check the adapter configuration for your project in database.yml. For example:
adapter: mysql
A comparison of the results of these checks between each machine should tell you if you are using different adapters on the two machines.