SSIS SQL statement error - sql-server

I created an SSIS package with source being a SQL query, destination being a flat file.
The SQL statement executes properly in SQL Server Management Studio, however it errors out when run from SSIS.
The statement is:
use Echo_active
select
DATEFROMPARTS(year(getdate()) - 1, month(getdate()) + 2, 1) as StartDate,
eomonth(DATEFROMPARTS(year(getdate()) - 1, month(getdate()) + 4, 1) ) as EndDate,
pd.[PA-PT-NO-WOSCD],
pd.[PA-PT-NO-SCD],
REPLACE(di.[PA-DX2-CODE],'.','') DiagCode,
REPLACE(STR(di.[PA-DX2-PRIO-NO], 2, 0),' ', 0) DiagSeqNo,
di.[PA-DX2-PRESENT-ON-ADM-IND] POA,
di.[PA-DX2-CODING-SYS-IND] DiagVersion
from
patientdemographics pd
left join
DiagnosisInformation di on pd.[PA-REGION-CD] = di.[PA-REGION-CD]
and pd.[PA-HOSP-CD] = di.[PA-HOSP-CD]
and pd.[PA-PT-NO-WOSCD] = di.[PA-PT-NO-WOSCD]
where
[pa-dsch-date] > = DATEFROMPARTS(year(getdate()) -1, month(getdate()) + 2, 1)
and [pa-dsch-date] <= eomonth(DATEFROMPARTS(year(getdate()) - 1, month(getdate()) + 4, 1))
and di.[PA-DX2-PRESENT-ON-ADM-IND] <> ''
order by
di.[PA-DX2-CODE]
The error message I am receiving is :
[Execute SQL Task] Error: Executing the query "
use Echo_active
select
DATEFROMPARTS(year(getda..." failed with the following error: "Cannot construct data type date, some of the arguments have values which are not valid.".
Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
How could the same statement execute in one place but errors out in the other?
Any suggestions would be greatly appreciated.

Related

Update SQL Server table using LEFT

I'm trying to update a table in sql server using the below command but I get the following error:
Msg 156, Level 15, State 1, Line 716
Incorrect syntax near the keyword 'LEFT'
Is LEFT not allowed when updating? What can be used instead? Thanks
UPDATE DI.DBO.MHS
SET (LEFT(BATCH_DATE_2, 1) + '0' + RIGHT(BATCH_DATE_2, 6))
WHERE LEFT(BATCH_DATE_2, 1) = 2
You must specify the column that you want to update:
UPDATE DI.DBO.MHS
SET BATCH_DATE_2 = LEFT(BATCH_DATE_2,1) + '0' + RIGHT(BATCH_DATE_2,6)
WHERE LEFT(BATCH_DATE_2,1) = 2
If it is not BATCH_DATE_2 the column that you want to update then use that column after SET.

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)) ;""")

What is incorrect in this sql query?

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'

Why do I get "Conversion failed when converting date and/or time from character string" when subtracting a number from GETDATE()?

I cannot figure out what I am doing incorrect here. I have the following values for these variables.
my $sql = qq~
SELECT COUNT(*)
FROM tableName u
WHERE 1=1 AND u.ManufacturerPartNumber IN ('X','Y','Z')
AND CAST(u.InspectionStartDate AS DATETIME) > (GETDATE() - ?)
~;
my $argsRef->{lookBack} = 30;
And when I try to run a selectrow_array on the sql like such:
my $qnCount = $dbh->selectrow_array($sql, undef, $argsRef->{lookBack});
I get the following error:
DBD::ODBC::db selectrow_array failed: [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting date and/or time from character string. (SQL-22007) [for Statement "
SELECT COUNT(*)
FROM tableName u
WHERE 1=1 AND u.ManufacturerPartNumber IN ('X','Y','Z')
AND CAST(u.InspectionStartDate AS DATETIME) > (GETDATE() - ?)
"]
So, it is my understanding that the third parameter in the selectrow_array call from a database handle should be the bind variables. Yet, they seem not to be binding....
the database handle is created as such:
my $dbh = DBI->connect_cached("dbi:ODBC:$dsn", undef, undef, {
PrintError => 0,
RaiseError => 1,
ShowErrorStatement => 1,
LongReadLen => 500000,
})
$dsn is the correct DSN but not shown for various reasons, but we know it works due the error message coming back from SQL Server.
Any idea what I am doing wrong?
Thanks in advance for your help.
First, a shout out to ThisSuitIsBlackNot for pointing out that the problem. I made the assumption that the DBI/driver would be able to determine the value type. That was wrong. So, I explicitly stated the type:
$sth = $dbh->prepare($sql);
$sth->bind_param( 1, ($argsRef->{lookBack} * -1), { TYPE => SQL_INTEGER });
$sth->execute();
my $qnCount = $sth->fetchrow_array();
This allowed it to go through as an integer and the SQL ran without problem.
In case you were wondering why I am multiplying by -1, is because I changed the query to use DATEADD:
SELECT COUNT(*)
FROM tableName u
WHERE 1=1 AND u.ManufacturerPartNumber IN ('x')
AND CAST(u.InspectionStartDate AS DATETIME) > DATEADD(dd, ?, GETDATE())
Although there seems to be some continuing discussion in regards to its use, it is working here.
Thank you all for your help.

Entity Framework 6 + Oracle "Where In" Clause

Despite our best efforts we have been unable to get Entity Framework (6.1.3) + Oracle Managed Data Access (12.1.2400) to generate an 'IN' clause when using contains in a where statement.
For the following query:
var x = Tests
.Where(t => new[] { 1, 2, 3}.Contains(t.ServiceLegId));
var query = x.ToString();
Using MS SQL (SQL Server) we see the following generated:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[TestRunId] AS [TestRunId],
[Extent1].[DidPass] AS [DidPass],
[Extent1].[StartTime] AS [StartTime],
[Extent1].[EndTime] AS [EndTime],
[Extent1].[ResultData] AS [ResultData],
[Extent1].[ServiceLegId] AS [ServiceLegId]
FROM [dbo].[Test] AS [Extent1]
WHERE [Extent1].[ServiceLegId] IN (1, 2, 3)
Using Oracle we instead see:
SELECT
"Extent1"."Id" AS "Id",
"Extent1"."TestRunId" AS "TestRunId",
"Extent1"."DidPass" AS "DidPass",
"Extent1"."StartTime" AS "StartTime",
"Extent1"."EndTime" AS "EndTime",
"Extent1"."ResultData" AS "ResultData",
"Extent1"."ServiceLegId" AS "ServiceLegId"
FROM "dbo"."Test" AS "Extent1"
WHERE ((1 = "Extent1"."ServiceLegId") OR (2 = "Extent1"."ServiceLegId") OR (3 = "Extent1"."ServiceLegId"))
This is a trivialized example of what we actually have to do. In the actual code base this list can get quite long so a series of 'OR' statements is resulting in very inefficient execution plans.
Has anyone encountered this scenario? I feel like we've tried everything...

Resources