Query join with SSIS parameter - sql-server

It is possible to use parameter (SSIS variable) after left join in query in OLE DB Source? The query looks like:
...
FROM X
LEFT JOIN ?
...
where "?" is string variable, which I dynamically changing (based on loop iteration).
When I put this string, except "?" - it works. When I deleted it and parametrized I got:
TITLE: Microsoft Visual Studio
Parameters cannot be extracted from the SQL command. The provider
might not help to parse parameter information from the command. In
that case, use the "SQL command from variable" access mode, in which
the entire SQL command is stored in a variable.
------------------------------ ADDITIONAL INFORMATION:
Syntax error, permission violation, or other nonspecific error
(Microsoft SQL Server Native Client 10.0)
------------------------------ BUTTONS:
OK
I use MS 2008R2.

Related

Incorrect syntax near Go with Pypyodbc

I am using the pypyodbc library to establish a connection to a SQL Server 2008 R2 database and every time I try to execute a .sql file I encounter the following error:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")
This is the sql query I am trying to execute:
Use SL_Site1_App
Go
select emp_num,name, trans_num, job, trans_type
from Hours where trans_type like '1000%' order by trans_date desc
This is the python script that I am using:
import pypyodbc, ExcelFile
def main():
# read the SQL queries externally
queries = ['C:\\Temp\\Ready_to_use_queries\\Connection_sql_python.sql']
for index, query in enumerate(queries):
cursor = initiate_connection_db()
results = retrieve_results_query(cursor, query)
if index == 0:
ExcelFile.write_to_workbook(results)
print("The workbook has been created and data has been inserted.\n")
def initiate_connection_db():
connection_live_db = pypyodbc.connect(driver="{SQL Server}", server="xxx.xxx.xxx.xxx", uid="my-name",
pwd="try-and-guess", Trusted_Connection="No")
connection = connection_live_db.cursor()
return connection
The workaround for this problem is to delete the Use SL_Site1_App Go line but I want to know if this is a known problem related to the pypyodbc library to process these lines and if so, where should I look to notify the developers about this issue.
GO is a batch separator used by sqlcmd and SSMS. It's not a T-SQL operator.
Considering you're using an application to connect to SQL Server, declare your database in the connection string, by adding database="SL_Site1_App", and then remove the USE and GO statements in your SQL Statement.

SSIS variables in the query

I have ssis package which uses the SQLserver 2012 and
have the variable : clientLastSync which holds datetime
and have the execute task
query as below
select * from clnt where opendt > convert(varchar, CAST (#[User::clientLastSync] as date),101)
If I execute the package, below error appears:
Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has
occurred. Error code: 0x80040E07. An OLE DB record is available.
Source: "Microsoft SQL Server Native Client 11.0" Hresult:
0x80040E07 Description: "Conversion failed when converting date
and/or time from character string.".
Can you suggest what is wrong here. Thanks
You pass variables in SSIS as a ?, and then define your variables in your parameter mapping pane.
I'm assuming that you aren't silly enough to be storing your dates as a varchar, thus your Direct input SQL in your Execute SQL Task becomes:
SELECT * FROM clnt WHERE opendt > ?;
Then, you need to go to your Parameter mapping pane and click Add. Select your variable's name (probably User::clientLastSync ) in the Variable name Column. Then Input as the direction. DBTIMESTAMP as your Data Type and ParameterName as 0 (if you had further parameters they would be 1, 2, 3, etc in the order they appear in your query). Leave Paramter Size as -1.
This should work as you intend now.

SQL Server 2005 query uses ? which doesn't work in SQL Server 2012

I'm working on an application which queries live data on SQL Server. The user enters a name within '% %' marks to search. Ie. if the user was to search for the owner of a property such as Noble, they would enter %noble%.
We recently upgraded both the application and the SQL Server that stores the data from SQL Server 2005 to SQL Server 2012.
The existing query and the new query are identical:
SELECT aurtvalm.pcl_num
FROM aurtvalm
INNER JOIN rtpostal ON aurtvalm.ass_num = rtpostal.ass_num
WHERE rtpostal.fmt_nm2 LIKE ?
In the old version, the above query produces 16 results. The exact same query in 2012 version produces an error:
Incorrect Syntax near '?'
Has the use of the ? symbol changed since SQL Server 2005?
That because you have incorrect syntax. You have to use parameter instead of question mark. Something like:
SELECT aurtvalm.pcl_num
FROM aurtvalm
INNER JOIN rtpostal ON aurtvalm.ass_num = rtpostal.ass_num
WHERE rtpostal.fmt_nm2 like #param

Generate UniqueIdentifier GUID in SSIS

I am using a GUID for a batch identifier in SSIS. My final output goes to SQL Server.
I know how I can generate one using Select NewId() MyUniqueIdentifier in Sql Server - I can generate one using a query and an Execute SQL task.
I am however looking to do this within a SSIS package if possible without SQL Server available.
Can I generate a GUID within SSIS?
I had a similar problem. To fix it, I created an SSIS "Composant Script" in which I created a "guid" output. The script VS C# code was the following :
Row.guid = Guid.NewGuid();
Finally, I routed the output as a derived column into my database "OLE DB Destination" to generate a guid for every new entry.
Simply do it in an Execute SQL Task.
Open the task
Under General -> SQL Statement, enter your query Select NewID() MyID in the "SQLStatement" field
Under General -> Result Set, choose "Single row"
Under Parameter Mapping, Enter your User::myID in Variable Name, "Input" as direction, 0 as Parameter Name, and -1 as Parameter Size
Under Result Set, enter "MyID" for your Result Name and type the variable in Variable Name
-Click OK
Done. Note that "MyID" is a value you can choose. EDIT: "User::myID" corresponds to the SSIS variable that you create.

Execute sql task mapping variables in ssis

INSERT INTO [DEV_BI].dbo.[DimAktivitet]([Beskrivning],[företag],[Projektnummer],[Aktivitet],
loaddate)
SELECT NULL,
a.DATAAREAID,
a.PROJID,
a.MID_ACTIVITYNUMBER,
GETDATE()
FROM [?].dbo.[v_ProjCostTrans_ProjEmplTrans] a
LEFT OUTER JOIN [DEV_BI] .dbo.[DimAktivitet] b ON a.MID_ACTIVITYNUMBER = b.Aktivitet
AND a.DataAreaID = b.företag
AND a.ProjID = b.Projektnummer
WHERE b.Aktivitet_key IS NULL
I have this above sql code in execute sql task and in the parameter mapping i have mapped a variable named user::connectionstring with data type nvarchar , parameter name = 0. Im getting this following error.
[Execute SQL Task] Error: Executing the query "insert into [DEV_BI].dbo.[DimAktivitet]([Beskrivni..." failed with the following error: "Invalid object name '?.dbo.v_ProjCostTrans_ProjEmplTrans'.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
please someone help me to solve this.
It appears you are trying to change the database based on a variable. The Execute SQL Task can only use parameters as filters in the WHERE clause. This behavior is described in this TechNet article. For instance, you could do this:
insert into [DEV_BI].dbo.[DimAktivitet]([Beskrivning],[företag],[Projektnummer],[Aktivitet],loaddate)
select null,a.DATAAREAID,a.PROJID,a.MID_ACTIVITYNUMBER,GETDATE() from
[DEV_BI].dbo.[v_ProjCostTrans_ProjEmplTrans] a
left outer join
[DEV_BI] .dbo.[DimAktivitet] b
on a.MID_ACTIVITYNUMBER = b.Aktivitet AND a.DataAreaID = b.företag AND a.ProjID = b.Projektnummer
where b.Aktivitet_key is null
AND b.SomeFilterCriteria = ?;
If you really want to vary the database based on a variable, then you have three options:
Vary the Connection Manager connection string to your database connection based on an expression as described in a blog post. This is the best solution if you are only changing the database and nothing else.
Generate the entire SQL code as a variable and execute a variable as the SQL command instead of passing variables to the Execute SQL Command. This is described in this blog post under the section "Passing in the SQL Statement from a Variable".
Create a stored procedure, pass the parameter to the stored procedure, and let it generate the SQL it needs on the fly.

Resources