I am trying to run sql written in Access on sql server. The query is throwing a syntax error on line 4:
UPDATE mytablename SET
table.[Specimen Collection Date 1] =
IIf(
[Specimen Collection Date 2] Is Not Null, //incorrect syntax near is
[Specimen Collection Date 2],
IIf([Specimen Collection Date 2] Is Null,[Specimen Collection Date 3] Is Not Null))
It seems like a valid column name [Speciment Collection Date] seems like a valid expression.
What am I missing?
There is no IIf in sql. Look up the case when tsql convention.
http://msdn.microsoft.com/en-us/library/ms181765.aspx
You don't need to include the TABLE in your column definition. The DB knows what table it is from your "Update TABLE" statement.
Related
I'm attempting to edit an ETL package(SSIS) that queries a SQL table and outputs csv files for every StationID and I'm having trouble understanding how the question mark is being used in the query definition below. I understand ? is used a parameter but I don't understand how it's used in the date function below:
SELECT TimeSeriesIdentifier, StationID, ParameterID FROM dbo.EtlView WHERE
LastModified > DATEADD(hour, ?*-1, GETDATE())
AND StationID LIKE
CASE WHEN ? = 0 THEN
StationID
ELSE
?
END
The parameterization available in SSIS is dependent upon the connection manager used.
OLE DB and ODBC based connection managers use ? as the variable place holder, whereas ADO.NET uses a named parameter, #myVariable.
OLE DB begins counting at 0 whereas ODBC used a 1 based counting system. They are both however ordinal based systems so in your CASE expression the two ? are for the same variable. But, you'll have to list that SSIS Variable twice in the parameter mapping dialog because it's ordinal based - i.e. (param, name) => #HoursBack, 0; #MyVar, 1; and #MyVar, 2;
A "dumb trick" I would employ if I had to deal with repeated ordinal based parameters or if I was troubleshooting packages is to make the supplied query use local variables in the query itself.
DECLARE
#HoursBack int = ?
, #MyVariable int = ?;
SELECT
TimeSeriesIdentifier
, StationID
, ParameterID
FROM
dbo.EtlView
WHERE
LastModified > DATEADD(HOUR, #HoursBack * -1, GETDATE())
AND StationID LIKE
CASE
WHEN #MyVariable = 0 THEN StationID
ELSE #MyVariable
END;
Now I only have to map the SSIS Variable #MyVar once into my script as the "normal" TSQL parameterization takes over. The other benefit is that I can copy and paste that into a query tool and sub in the ?s with actual values to inspect the results directly from the source. This can be helpful if you're running into situations where the strong typing in SSIS prevents you from getting the results into a data viewer.
SSIS is building a parameterized query.
You can get more information about this here (MySQL-specific):
What is the question mark's significance in MySQL at "WHERE column = ?"?
Or you can get a more generally-applicable response here: What does a question mark represent in SQL queries?
At a very "nuts and bolts" level, those are parameters being passed into the SQL statement by the package. With the Execute SQL task open, click on the tab that says Parameter Mapping. There will be a list of variables that are being sent into the query, and they are consumed in the order that they're listed.
Here's a logger for an archiving package I'm working on:
The query on the General tab just writes those five values to a table:
INSERT INTO dbo.ArchiveRowCounts (
TableName,
ServerName,
ReportYear,
BaseTblCnt,
ArchiveTblCnt)
VALUES (?,?,?,?,?);
I'm using the mssql server to implement a spatial database. I have one table (Ways) containing a geography column called geoline, that is of LINESTRING type.
I want to select the geoline containing a specific POINT, with the coordinates (38.731611,-9.135336).
I tried this:
SELECT geoLine.STAsText()
FROM Ways
WHERE geoLine.STContains(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0));
But it returned the following error:
Msg 4145, Level 15, State 1, Line 2 An expression of non-boolean type
specified in a context where a condition is expected, near ';'.
What can I do? Any idea for a success query?
Thanks
I think you are using SQL SERVER 2008 or below thats why you are getting that error because
STContains (geography Data Type) will work from SQL SERVER 2012 and above (source).
In your case to find instance completely contains another geometry instance you need a geometry data type.
FIDDLE DEMO HERE
CREATE TABLE #Ways
(geoLine GEOMETRY)
INSERT INTO #Ways
(geoLine)
VALUES (geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 0)),
(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0))
SELECT geoLine.STAsText() AS geoLine,geoLine
FROM #Ways
SELECT geoLine.STAsText() AS geoLine
FROM #Ways
WHERE geoLine.STContains(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0)) = 1
According to T-SQL documentation, STContains returns a BIT data type, so your WHERE condition should look like this I think :
SELECT geoLine.STAsText()
FROM Ways
WHERE geoLine.STContains(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0)) = 1;
I'm using hibernate with sql_server, but am finding problems with the following:
select col_code
from table
where col_code is null
Query fails: sql error 0, SQLState S1093 The column col_code is not valid
col_code is a char(5), and reads okay from other database browsers. Its just from hibernate - Any Ideas?
I got the same error when I was doing an native SQL query from Java EE/Hibernate. The SQL statement that I passed to persistence was "SELECT Count (1) From Table" and I made the mistake of including a mapping class as a parameter. COUNT(1) returns an integer, so it doesn't need to be mapped. Hope that helps.
I would check that you're mapping is right.
I need some help. I am suppose to get XML data from column of a table. The XML data has many tags. One of the tag contains values like
<Code date= 30/03/2013>
<apr id =1> -2 </apr>
<rdv id =2> 1 </rdv>
</code>
I need to run a particular task which checks the date. Like if today's date= (Code date -2) or
today's date = (code date + 1)
run a mailer task.
How should I go about it ? Please forgive me for the XML data format. I am naive in XML.
Create 2 variables to store the XML column Date value and other one to get the current date .To write an expression ,select the variable ,right click on it .In the properties windows Set EvaluateAsExpression =true and write the expression
Name DataType Expression
ExecDate DateTime
CurrentDate DateTime GETDATE()
Use Execute SQL Task .Set Resultset to Single Row and write the following code to read the date value from the XML column in your table
SELECT
convert(datetime,XMLData.value('(/Code/#date)[1]', 'varchar(10)'),103) as PkgDate
from yourTable
Demo in SQL Fiddle
In the resultset tab map the output from the above sql query with the variable ExecDate
Result Name Variable Name
PkgDate ExecDate
Put another Task or component Ex SEND Mail TASK after Execute SQL Task and write an expression in the Precedence Constraint.
Expression : #ExecDate==#CurrentDate
Only when the Date value from the XML column matches with the Current Date which is stored in the variable #CurrentDate then only the other components after Execute SQL Task will execute .
today's date= (Code date -2) or today's date = (code date + 1) run a mailer task.
In order to perform the above expression ,you need to change the sql code in Execute SQL Task
To get CodeDate +1
SELECT
Dateadd(day,1,convert(datetime,XMLData.value('(/Code/#date)[1]', 'varchar(10)'),103)) as PkgDate
from yourTable
I don't have the SSIS environment to test it now but this is how you should be doing it
I've got a query like
select x from tableName where startDate <= now;
When i query the database without the date fields, everything works as expected. As soon as i start using date or timestamp columns in the oracle Database, my queries return nothing or an error.
What I do:
snprintf(sql, sizeof(sql), "SELECT roomNo, userPass, adminPass, adminFlags, userFlags, bookId, is_locked, running_on_server FROM booking WHERE roomNo = '?' AND startTime <= { ts '?' } AND endTime >= { ts '?' } for update;");
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
the ? will be replaced by values, with following command:
SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(gps->argv[i]), 0, gps->argv[i], 0, NULL);
when I execute the query I get an error that TS is an invalid identifier [this was a recommendation by another board, taken from msdn - which may cause this error]
but even if I remove it and send just a string to the database, I'll get an empty result back. I also tried to bind the parameters as SQL_TIMESTAMP and SQL_DATE, but this didn't help either.
Hopefully somebody can help me.
thanks in advance.
Chris
Are you sending a DATE data type to the Oracle query or a date represented in a string?
From the Oracle side of things, if you send a date in a string variable you'll need to use the oracle "TO_DATE" function (http://www.techonthenet.com/oracle/functions/to_date.php) to then convert thew string back to a date for use in your SQL statement (assuming startDate or startTime/endTime are DATE columns in the database).
Your fist SQL example should be:
SELECT x
FROM tableName
WHERE startDate <= TO_DATE(now, '<date-format>');
If the variable "now" was a string containing '05-OCT-2011 16:15:23' (including a time portion) then the to_date would be:
TO_DATE(now, 'DD-MON-YYYY HH24:MI:SS')
If you compare a string with a date and don't specify the format of that date Oracle will use its default NLS parameters and try to apply that format. Therefore it is always prudent to specify the date format using TO_DATE.
Hope it helps...