What is incorrect in this sql query? - sql-server

update v, s
set v.closed = 'Y'
where v.closed <> 'y'
and v.canceldate < '12.01.2017'
and s.salesrep1 = 'bd'
and v.orderno = s.orderno
This is my query and I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.

The problem is most likely that you mixed up what rdbms product you use. The error message is from MS SQL Server, while the question is (well, was) tagged as mysql.
The syntax used in the question is allowed in mysql, but is not allowed in MS Sql Server, hence the error message.
In MS Sql Server try the following syntax:
update v set v.closed = 'Y'
from v inner join s
on v.orderno = s.orderno
where v.closed <> 'y'
and v.canceldate < '12.01.2017'
and s.salesrep1 = 'bd'
See ms sql server reference on update statement for details

I would pur it like this (on sql server)
update v
set v.closed = 'Y'
From v inner join s
On v.orderno = s.orderno
Where v.closed <> 'y'
and v.canceldate < '12.01.2017'
and s.salesrep1 = 'bd'

Related

I have a Pymmsql error in python Query processor could not produce a query plan

I have a issue reading my index select statement with this error any solution:
File "src\pymssql.pyx", line 468, in pymssql.Cursor.execute
pymssql.OperationalError: (8622, b'Query processor could not produce a query plan because of the
hints defined in this query. Resubmit the query without specifying any hints and without using SET
FORCEPLAN.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from
the SQL Server\n')
Code :
with pymssql.connect ("*********","*********", "**********","*****") as myDbConn:
with myDbConn.cursor() as cursor:
cursor.execute(""" if exists (select * from sys.indexes where name = 'Micros' and object_id('payrolldata') = object_id)
begin
drop index Micros on payrolldata
end """)
sql = """create index Micros on payrolldata(stono,payrollid,busdate)where busdate >= 's' and busdate <= 's' """ .format(dteStartDate,m0weekend);
cursor.execute(sql)
DbConnect = 'Micros'
myDbConn = pymssql.connect("*******","*******", "********",*******)
cursor = myDbConn.cursor()
cursor.execute("""select * from payrolldata with(INDEX(Micros)) ;""")

Boolean expressions in SQL Server jobs

I have inherited a SQL Server "job" that does several things. There are two "steps" and each has multiple statements one is:
UPDATE Person
SET Person.LastName = P.LastName,
Person.FirstName = P.FirstName,
Person.MiddleName = P.MiddleName,
Person.EmailAddress = P.EmailAddress,
Person.StartDate = P.StartDate,
Person.EndDate = P.EndDate
FROM OtherDB.dbo.Person Person
INNER JOIN FirstDB.dbo.Persons AS P ON P.PersonId = Person.PersonId
WHERE Person.LastName != P.LastName
OR Person.FirstName != P.FirstName
OR Person.MiddleName != P.MiddleName
OR Person.EmailAddress != P.EmailAddress
OR Person.StartDate != P.StartDate
OR Person.EndDate != P.EndDate;
It is updating "person" data from FirstDB into OtherDB. The PersonId columns are bigints and are not null. The various "date" columns are of type datetime and could be NULL. The other columns are all varchar and could be NULL.
What I have learned is that, in the where clause, if NULL appears on either or both sides of the boolean operator the result is undefined. Basically, NULL can neither equal nor not-equal NULL. It appears that the same applies to NULL and any other non-null value.
So I thought to try:
UPDATE Person
SET Person.LastName = P.LastName,
Person.FirstName = P.FirstName,
Person.MiddleName = P.MiddleName,
Person.EmailAddress = P.EmailAddress,
Person.StartDate = P.StartDate,
Person.EndDate = P.EndDate
FROM OtherDB.dbo.Person Person
INNER JOIN FirstDB.dbo.Persons AS P ON P.PersonId = Person.PersonId
WHERE ISNULL(Person.LastName, '') != ISNULL(P.LastName, '')
OR ISNULL(Person.FirstName, '') != ISNULL(P.FirstName, '')
OR ISNULL(Person.MiddleName, '') != ISNULL(P.MiddleName, '')
OR ISNULL(Person.EmailAddress, '') != ISNULL(P.EmailAddress, '')
OR ISNULL(Person.StartDate, '') != ISNULL(P.StartDate, '')
OR ISNULL(Person.EndDate, '') != ISNULL(P.EndDate, '');
This works in a regular query window but fails in the job. The error is:
An expression of non-boolean type specified in a context where a condition is expected, near 'OR'. [SQLSTATE 42000] (Error 4145). The step failed.
I am not seeing a problem. What am I missing?
Edit
As requested, for others in this situation: I edited this job by using SQL Server Management Studio. I opened a connection to my target DB then opened the "SQL Server Agent" drop-down under the connection. I opened "Jobs" and found the job i was looking for. I right-clicked on the job name and selected "Script Job As" -> "Drop and Create to" -> "New Query Window". From there I copied the relevant sections to new query windows where I modified and tested them as necessary. I then just copied and pasted the working sections back into the job window above -- COMPLETELY forgetting to double up the single quotes where necessary.
How did you add the step to the job? I wonder if a script you used escaped all of your double apostrophes and it is now trying to evaluate
WHERE ISNULL(Person.LastName, ') != ISNULL(P.LastName, ')
------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^
That's "valid enough" syntax. The highlighted portion is a simple string literal, not an empty string and a comparison to another expression.

SQL Server error 80040e14 Using Classic ASP in a Result Set

I'm trying to select results from a database for each position of a vector using classic ASP and SQL Server. The code so far:
FOR EACH x IN Tabela
sql = "SELECT DISTINCT tborders.family AS family, tborders.qty AS qty, tborders.los AS los, CONVERT(DATE, tborders.mrd_date) AS mrd FROM [DASH].[dashboard_db].[dbo].[tb_family] AS tbfamily INNER JOIN [DASH].[dashboard_db].[dbo].[tb_started_zero] AS tborders ON tbfamily.[family] = tborders.[family] WHERE tborders.[Order Number] = "&x&""
SET rs = conn.execute(sql)
IF rs.EOF = false THEN
mrd(counter) = rs("mrd")
family(counter) = rs("family")
los(counter) = rs("los")
qty(counter) = rs("qty")
counter=counter+1
END IF
NEXT
Taking note that tborders.[Order Number] is a int typed value. For some reason I'm having this error:
Microsoft SQL Server Native Client 11.0 error '80040e14'
Incorrect syntax near '='.
/asplearning/act/validate-schedule-line.asp, line 46
I have tried removing the SET, but then my result set isn't recognized as a object. I'm pretty sure that the types are just fine, I have tried:
if isNumeric(x) THEN
response.write("<h1>it is numeric</h1>")
else
response.write("<h1>not numeric</h1>")
end if
And it has written "it is numeric" for each position of Tabela. Can anyone help with what it seems to be the problem?
It's look you have an empty item (first or last item of the collection).
I strongly suggest you to use sp_executesql. This will use a compiled execution plan, and this will validate your parameters (against sql injection).
FOR EACH x IN Tabela
if len(x) > 0 then
sql = "exec sp_executeSql N'SELECT DISTINCT tborders.family AS family, tborders.qty AS qty, tborders.los AS los, CONVERT(DATE, tborders.mrd_date) AS mrd FROM [DASH].[dashboard_db].[dbo].[tb_family] AS tbfamily INNER JOIN [DASH].[dashboard_db].[dbo].[tb_started_zero] AS tborders ON tbfamily.[family] = tborders.[family] WHERE tborders.[Order Number] = #OrderNumber', N'#OrderNumber int', #orderNumber = " & x
SET rs = conn.execute(sql)
IF rs.EOF = false THEN
mrd(counter) = rs("mrd")
family(counter) = rs("family")
los(counter) = rs("los")
qty(counter) = rs("qty")
counter=counter+1
END IF
rs.close
end if
NEXT

Mass Updates in SQL Server

I am trying to do a mass update on short codes in SQL Server, but it doesn't return any change:
SELECT DISTCINT
a.site_area_sc, a.site_area_n, a.site_area_rmk, d.site_n 'NAME', d.site_sc,
UPDATE DB.DBO.bg
SET bg_sc = 'XXX-' + bg.bg_sc
FROM DB.DBO.bg
INNER JOIN DB.DBO.site ON s.site_id = bg.site_id
WHERE site.site_sc like 'XXX-%'
AND bg.stat_flag = 'n'
AND s.stat_flag = 'n'
AND bg.bg_sc not like 'XXX-%'
I am in need of help.

The server failed to resume the transaction. Linq-To-SQL

I'm receiving a System.Data.SqlClient.SqlException: The server failed to resume the transaction. Desc:6c00000001 when executing a Linq-To-SQL query.
Here is my repository call:
using (var ctx = new EntitiesDataContext())
{
ctx.ObjectTrackingEnabled = false;
ctx.DeferredLoadingEnabled = false;
var loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Company2QualifierLicense>(n => n.QualifierLicense);
loadOptions.LoadWith<Company2QualifierLicense>(n => n.Company);
loadOptions.LoadWith<QualifierLicense>(n => n.QualifierLicenseHoldStatus);
loadOptions.LoadWith<QualifierLicense>(n => n.LicenseTrade);
loadOptions.LoadWith<Company>(n => n.CompanyHoldStatus);
ctx.LoadOptions = loadOptions;
return ctx.Company2QualifierLicenses.Where(p => p.QualifierLicense.QualifierLicenseNumber == qualifierLicense).ToList();
}
Here is the SQL generated:
-- Region Parameters
DECLARE #p0 VarChar(1000) = '11223344'
-- EndRegion
SELECT [t0].[CompanyID], [t0].[QualifierLicenseID], [t0].[InitiatedDate], [t0].[IsActive], [t0].[RowVersion], [t0].[LastUpdated], [t1].[QualifierLicenseID] AS [QualifierLicenseID2], [t1].[QualifierLicenseNumber], [t1].[LicenseTradeID], [t1].[LicenseExpirationDate], [t1].[FirstName], [t1].[LastName], [t1].[MailingAddress1], [t1].[MailingAddress2], [t1].[City], [t1].[StateAbbr], [t1].[ZIP], [t1].[Email], [t1].[Phone], [t1].[RowVersion] AS [RowVersion2], [t1].[LastUpdated] AS [LastUpdated2], [t3].[test], [t3].[LicenseTradeID] AS [LicenseTradeID2], [t3].[LicenseCode], [t3].[LicenseDescription], [t5].[QualifierLicenseHoldStatusID], [t5].[HoldReasonID], [t5].[QualifierLicenseID] AS [QualifierLicenseID3], [t5].[RowVersion] AS [RowVersion3], [t5].[LastUpdated] AS [LastUpdated3], (
SELECT COUNT(*)
FROM [frontdesk].[QualifierLicenseHoldStatus] AS [t6]
WHERE [t6].[QualifierLicenseID] = [t1].[QualifierLicenseID]
) AS [value], [t4].[CompanyID] AS [CompanyID2], [t4].[EIN], [t4].[CompanyName], [t4].[MailingAddress1] AS [MailingAddress12], [t4].[MailingAddress2] AS [MailingAddress22], [t4].[City] AS [City2], [t4].[StateAbbr] AS [StateAbbr2], [t4].[ZIP] AS [ZIP2], [t4].[Email] AS [Email2], [t4].[Phone] AS [Phone2], [t4].[RowVersion] AS [RowVersion4], [t4].[LastUpdated] AS [LastUpdated4]
FROM [frontdesk].[Company2QualifierLicense] AS [t0]
INNER JOIN ([frontdesk].[QualifierLicense] AS [t1]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t2].[LicenseTradeID], [t2].[LicenseCode], [t2].[LicenseDescription]
FROM [frontdesk].[LicenseTrade] AS [t2]
) AS [t3] ON [t3].[LicenseTradeID] = [t1].[LicenseTradeID]) ON [t1].[QualifierLicenseID] = [t0].[QualifierLicenseID]
INNER JOIN [frontdesk].[Company] AS [t4] ON [t4].[CompanyID] = [t0].[CompanyID]
LEFT OUTER JOIN [frontdesk].[QualifierLicenseHoldStatus] AS [t5] ON [t5].[QualifierLicenseID] = [t1].[QualifierLicenseID]
WHERE [t1].[QualifierLicenseNumber] = #p0
ORDER BY [t0].[CompanyID], [t0].[QualifierLicenseID], [t1].[QualifierLicenseID], [t3].[LicenseTradeID], [t5].[QualifierLicenseHoldStatusID]
GO
-- Region Parameters
DECLARE #x1 Int = 241
-- EndRegion
SELECT [t0].[CompanyHoldStatusID], [t0].[CompanyID], [t0].[HoldReasonID], [t0].[RowVersion], [t0].[LastUpdated]
FROM [frontdesk].[CompanyHoldStatus] AS [t0]
WHERE [t0].[CompanyID] = #x1
As you can see I'm creating and disposing of the DataContext immediately after the database query, so no further calls can be made from the calling method.
I see that there are two queries issued to the database and my guess is that when issuing the second query to the database the transaction has been committed, but Linq-To-SQL should be smarter than that.
I'm using .NET 4.0 and SQL Server 2008 R2 (SP1) - 10.50.2789.0
Any ideas?
UPDATE Dec/21/2011
Here is another piece of the exception:
The transaction active in this session has been committed or aborted by another session
UPDATE Dec/30/2011
A coworker found that this issue has been reported to Microsoft and it has been confirmed as a bug but it won't be fixed, and their recommendation is to move to Entity Framework.
A coworker found that this issue has been reported to Microsoft and it has been confirmed as a bug but it won't be fixed, and their recommendation is to move to Entity Framework.
A bit too long for comments.
You need to realize that eager loading in linq2sql does not work on multiple levels in a tree. So load b with a and c with b does not load a-b-c in one go.
http://www.lowendahl.net/showShout.aspx?id=190
This explains your additional second query.
Now why you get the exception I do not know.

Resources