I have got a SQL command like the following :
SELECT *
FROM temp1 a
inner join (SELECT ID from temp2 where ID = ?) b on a.ID = b.ID
WHERE a.ID = ?
I know the query has incorrect logic. But my question is How will I set Variables as parameters here in the place of "?". If I exclude the joined part, it will work.
Edit the OLE DB Source. On the Connections Manager tab, click the Parameters... button. The Parameters dialog will display the parameters in the sequence they appear in the SQL statement. The sequence is determined by reading the SQL statement from left-to-right, top-to-bottom.
To make an assignment to a parameter, select the variable from the Variables column.
Related
I know SQL in SQLite is not completely implemented the same way as in MySql. My problem with the following queries is, that they are not compatible and I like to avoid a conditional if <DBMS> ... else
SQLite query
UPDATE sorties SET state = '#'
WHERE `key` IN (
SELECT `key` FROM sorties
INNER JOIN reports AS r
ON r.sortieId=sorties.`key`);
Error on MySQL:
SQL Error (1093): Table 'sorties' is specified twice, both as a target for 'UPDATE' and as a separate source for data
MySQL query (adapted from here)
UPDATE sorties AS s SET s.state='#'
WHERE s.`key` IN (
SELECT t.sortieId FROM (
SELECT r.sortieId AS sortieId
FROM reports AS r
INNER JOIN sorties AS sort
ON sort.`key`=r.sortieId)
AS t);
Error on SQLite:
SQLiteManager: Likely SQL syntax error: UPDATE sorties AS s SET s.state='#'
WHERE s.key IN ( SELECT t.sortieId FROM (
SELECT DISTINCT r.sortieId AS sortieId
FROM reports AS r
INNER JOIN sorties AS sort
ON sort.key=r.sortieId) AS t); [ near "AS": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
I can't figure out how to make this queries work on both systems equally!
All I want to have is, that each state of sorties must be '#' when it's key can be found in reports.sortieId.
Maybe there is a different approach for this?
Thank you
The first command reads the key value from the sorties table in the subquery, and then checks whether those key values exist in the sorties table in the outer statement. That check is superfluous; you can just compare the values to the ones in reports directly:
UPDATE sorties
SET state = '#'
WHERE key IN (SELECT sortieId
FROM reports);
As for the second command, SQLite does not support aliasing a table used in INSERT/UPDATE/DELETE because those commands work only on a single table. You can just remove the AS s and replace s with sorties everywhere.
For a projet, I retrieved the database from a client. This database contains tables that have attributes composed by several words that are concatenated separated by a point. exemple :
table A : id, number.client , global.score
table B : id, favorite.style
So when I do a sql request on this tables to filter to filter the results according to number.client for exemple, I have an error returned by MySQL.
For exemple I have this request :
SELECT * FROM A
INNER JOIN B
ON (A.ref_track = B.id)
INNER JOIN C
ON (C.id = B.ref_plannode)
WHERE (B.id= 1)
AND (A.number.client > 50)
ORDER BY A.id DESC
When I run this request I get this error :
MySQL replied:
#1064 - Syntax error on A.number.client > 50
I think that MySql don't failed when we have an attribute composed by servel words separated by point ( like number.client). So what is the solution ??
For inforamtion this database is out of my responsibility, I got it from a client!!.
You have to quote the columns names with "`"
SELECT *
FROM A
JOIN B
ON A.ref_track = B.id
JOIN C
ON C.id = B.ref_plannode
WHERE B.id= 1
AND A.`number.client` > 50
ORDER BY A.id DESC
Edit
Working example
Is the same case that white spaces, you can use this:
SELECT * FROM A
INNER JOIN B
ON (A.ref_track = B.id)
INNER JOIN C
ON (C.id = B.ref_plannode)
WHERE (B.id= 1)
AND (A.`number.client` > 50)
ORDER BY A.id DESC
The MySQL can use diferent quotes or braquets, depends of configuration.
The MySQL server can operate in different SQL modes, and can apply these modes differently for different clients, depending on the value of the sql_mode system variable.
You can view the configuration with this command:
SELECT ##GLOBAL.sql_mode;
SELECT ##SESSION.sql_mode;
And you can know more about the configuration of mysql here https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi_quotes
Try to quote the column name putting it between square brackets.
So
AND (A.[number.client] > 50)
in my dataflow task I have OLEDB Source with the following query:
SELECT am.Code AS MeterCode
,am.Code AS NewMeterCode
,am.Description AS NewMeterDescription
,mr.TagName
,mr.RunHours
,mr.DateTo AS DateRead
FROM [19-AIS-004\VIJEOHISTORIAN].VijeoHistorianCPSData.dbo.RunHours AS mr
INNER JOIN dbo.astMeters AS am ON mr.TagName COLLATE SQL_Latin1_General_CP1_CI_AS = am.Code
the table 'mr' is the linked server table where i'm getting the Tagname, Runhours and DateTo and join it with astMeters table in order to import data from mr table to am table.
my problem is since the package is deployed in the remote server via vpn I need a way which I can created string parameter for the this table '[19-AIS-004\VIJEOHISTORIAN].VijeoHistorianCPSData.dbo.RunHours'.
Build your entire query as an SSIS string variable, and in your OLEDB Source, choose SQL Query From Variable and select the string variable that holds your query.
I am running into problems with SSIS(2012), mysql(odbc) and dataflow when my sql command is more than one statement. In addition I cannot start the statement with a comment either. This is not a problem when using oledb & sql server.
SELECT id FROM id_table WHERE type = 'product' INTO #id;
SELECT name FROM products WHERE id = #id;
This isn't the most relevant example. It's just to show what I'm doing. But this will not work with Mysql(ODBC) as my source. I get the following:
Table has no columns
My work around is using a sub query:
SELECT name FROM products WHERE id = (SELECT id from id_table WHERE type = 'product');
Also the below with a comment comes up with the same error:
/* Test comment */
SELECT name FROM products WHERE id = (SELECT id from id_table WHERE type = 'product');
Again, sql server(OLEDB), haven't tried ODBC, has no problems with either case.
My first thought is to check the driver and enable the "Multiple statements" flag, which is already checked. Is there a solution to this in the settings of the driver or in SSIS?
EDIT:
Additional examples:
This works with sql server and odbc source for a dataflow:
DECLARE #offer AS VARCHAR(8)
SET #offer = 'WDWD' + CONVERT(VARCHAR,1000 + DATEPART(wk,GETDATE()))
SELECT * FROM source WHERE cd = #offer
The rough equivalent with mysql and odbc source for a dataflow does not:
SET #offer = CONCAT('WHW410', LPAD(WEEKOFYEAR(NOW()), 2, '0'));
SELECT * FROM source WHERE cd = #offer;
I verified both of these are valid sql in SSMS and sqlyog.
I've been trying to show a specific type of result in SQL but haven't been able to. I guess it's possible, but can't figure out how.
I've read this article show only those columns which have data value but couldn't solve my problem.
I have 4 tables: sparametros (holds parameters codes and descriptions), sparametrosnumericos (holds numeric parameters values and their code), sparametrostexto (holds text parameters and their values), sparametrosmemo (holds memo type parameters)
All of them can be joined by their parameter code, however, a parameter code is unique in the sense that for example, given a code, let's say 1210, and let's suppose it's a text type parameter, then that code doesn't exist in numeric nor in memo parameters either. However it exists in the general sparametros table. In other words, sparametros holds all parameters, and the other tables represent sub sets of that main set.
I've tried using left join, but couldn't get results.
This is what I have so far:
SELECT P.SPar00Id, P.SPar00Descripcion,
IF NOT ISNULL(N.SPar00NumValor) THEN
N.SPar00NumValor
ELSEIF NOT ISNULL(T.SPar00TextoValor) THEN
T.SPar00TextoValor
ELSE
M.SPar00MemoValor
FROM sparametros p
LEFT JOIN sparametrosnumericos N ON N.SPar00NumId = P.SPar00Id
LEFT JOIN sparametrostexto T ON T.SPar00TextoId = P.SPar00Id
LEFT JOIN sparametrosmemo M ON M.SPar00MemoId = P.SPar00Id
WHERE P.SIns00Id = 1 AND
N.SIns00Id = 1 AND
T.SIns00Id = 1 AND
M.SIns00Id = 1;
I'm using MySQL now (with the Navicat client), but also need to be able to get the same results in SQL Server.
The response I'm getting when executing this request is:
"[Err] 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 'NOT ISNULL(N.SPar00NumValor) THEN "
Try this:
SELECT P.SPar00Id, P.SPar00Descripcion,
(CASE WHEN N.SPar00NumValor IS NOT NULL THEN N.SPar00NumValor
WHEN T.SPar00TextoValor IS NOT NULL THEN T.SPar00TextoValor
ELSE M.SPar00MemoValor
END) colName
FROM sparametros p
LEFT JOIN sparametrosnumericos N ON N.SPar00NumId = P.SPar00Id
LEFT JOIN sparametrostexto T ON T.SPar00TextoId = P.SPar00Id
LEFT JOIN sparametrosmemo M ON M.SPar00MemoId = P.SPar00Id
WHERE P.SIns00Id = 1 AND N.SIns00Id = 1 AND T.SIns00Id = 1 AND M.SIns00Id = 1;