I am new to SQL Server 2012, and I found SQL Server Management Studio is a little bit difficult to use.
Here is one of the problems that I cannot solve: Is it possible to pass values to a query in SQL Server Management Studio ?
SELECT col1, col2
FROM table
WHERE col3 = :variable
If I run this query in the Studio, instead of prompting an input field to let me input value for variable, it will not execute the query because of an error
Incorrect syntax near ':'
I used to use the same method in SQL Developer for Oracle, thus I thought it would work the same.
Thanks for any useful advice.
DECLARE #someVariable varchar(n)
SET #someVariable = 'variable'
SELECT col1, col2
FROM table
WHERE col3 = someVariable
Related
I have a query in SSRS where I can search for a title with a certain prefix. The reason why I used left() is because there are some prefixes that have an underscore or a dash so I need my search results to be exact and it wasn't giving me that when I used the like operator.
Here's an example of what my query looks like:
select title
from table1
where left(title, len(#prefix)) = #prefix
This works if my prefix dropdown doesn't allow multiple values. Now, I have also tried this query:
select title
from table1
where left(title, len(#prefix)) in (#prefix)
But this just caused my report to generate a "Query execution failed for dataset" however, this query does work when I try it on SQL Server and when it isn't allowed for multiple values.
As I mentioned in my comment, I hate that SSRS allows the syntax IN (#Parameter); it causes misunderstandings and teaches people incorrect syntax.
When you have statement like SELECT * FROM dbo.MyTable WHERE MyColumn IN (#MyParameter); the SQL that SSRS runs is not that statement. Instead SSRS removes the parameter and then injects the multiple values into the statement. So the statement it actually runs would be something like SELECT * FROM dbo.MyTable WHERE MyColumn IN (N'MyValue1',N'MyValue2',N'MyValue3');
If you were to actually run the SQL SELECT * FROM dbo.MyTable WHERE MyColumn IN (#MyParameter); in SSMS, or any other product, it would be a synonym of SELECT * FROM dbo.MyTable WHERE MyColumn = #MyParameter;. The syntax that SSRS uses is, for all intensive purposes, wrong.
There are multiple problems with that, which I'm not going to cover here.
When you then have a query like you do LEFT(title, LEN(#prefix)) IN (#prefix) you end up with a problem; LEN(#prefix) can't have tuples injected into it (LEN(N'MyValue1',N'MyValue2',N'MyValue3') isn't valid syntax). This just results in a invalid query because SSRS teaches bad habits.
The solution to this is th force SSRS to not be (completely) dumb. Unfortunately SSRS is dumb and still (even in SSRS 2022!) doesn't support Table Table Parameters (TVPs) which would trivialise this. As such you'll need to have SSRS pass a delimited value to a Procedure, and then you can handle the logic in your procedure. As you're using SSRS 2019 then you're also using SQL Server 2019 and thus have access to STRING_SPLIT. Also we can switch to a LIKE here.
This means you need a procedure like the following:
CREATE PROC dbo.YourProc #prefix nvarchar(4000) --Assumed data type
AS
BEGIN
SELECT title
FROM dbo.table1 t1
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT(#prefix,',') SS
WHERE t1.title LIKE SS.value + '%');
END;
Then, instead, you execute the procedure from your report.
The following request is correctly written for SQL Server, but not for Oracle.
SELECT col1 AS 'col1_new_title',
col2 AS 'col2_new_title',
col3
FROM table
For Oracle, it needs to be an equal sign, with the new name in front.
Is there a way to write that request so that it's executable in both environments, without having to rewrite it each time?
The reason that your statement is not working in Oracle is caused by the single quotes. In SQL identifiers need to be enclosed in double quotes if you want them case sensitive. Single quotes are for string constants.
So the following
SELECT col1 AS "col1_new_title",
col2 AS "col2_new_title",
col3
FROM table
should work on both systems (actually on any DBMS that follows the SQL standard rules for identifiers)
For SQL Server this requires the configuration option "ANSI_QUOTES" (or something similar I don't recall the exact name) to be set to ON - which I think is the default in any recent SQL Server version.
If you don't care about UPPER/lowercase identifiers simply don't use any quotes at all:
SELECT col1 AS col1_new_title,
col2 AS col2_new_title,
col3
FROM table
SELECT
col1 col1_new_title
, col2 col2_new_title
, col3
FROM table
If you have no spaces and avoid reserved words then the barebones approach above would work in most SQL dialects.
Note oracle does not require as much and in fact does not accept it in some circumstances such as table aliases.
If this bare approach doesn't work add double quotes to the column aliases.
I have a database on SQL Server and would like to use a column in one of my tables in a linked server openquery I'm running to an Oracle database in order to match values between the two and insert the result into columns in my table in SQL Server .
Essentially I want it to be like this:
SELECT col1, col2, col3, col4
FROM OPENQUERY(link, 'SELECT * FROM Oracle_Table
WHERE ID = MSSQL.dbo.table.ID`)
So I'd like to be able to use my internal table column values to query an external database. They are related tables but different systems.
Would it be possible to get a big list of the values in the SQL Server table column and use it as a variable in the Oracle query? I've searched extensively online but haven't been able to find this one.
You can't pass parameters like I wanted to, but I ended up creating a bunch of queries in Powershell using a for loop and variables within the string to create my large query, then put a UNION ALL after each SELECT FROM OPENQUERY()
Is it possible to have something like this :
MOVE "'xx','yy','mm''" TO WS-TYPE-PARAM;
Then in SQL Query :
SELECT bar
FROM tblfoo
WHERE bar_type NOT IN (:WS-TYPE-PARAM)
I tried but unsucess. Maybe there's an alternative.
Thanks in advance.
99% Sure the answer is no. (With RPG that would be 100%)
You have to pass in a variable for each value
SELECT bar FROM tblfoo WHERE bar_type NOT IN (:WS-TYPE-PARAM1 : WS-TYPE-PARAM2 : WS-TYPE-PARAM3)
You could do it with dynamic SQL (sorry it's RPG)
wSqlStmt = 'SELECT bar FROM tblfoo WHERE bar_type NOT IN (' + WS-TYPE-PARM + ')';
Normally however, such dynamic SQL is a bad idea. For both performance and security reasons;
The above dynamic code is susceptible to SQL Injection attacks. But as long as the values in WS-TYPE-PARM are not coming from a user it's safe.
The last option would be to insert the values into a temporary table, then just
SELECT bar FROM tblfoo WHERE bar_type NOT IN (SELECT type_parm FROM tmptable)
I am trying to use the vs2008 query builder to create a query with a parameter.
I know that in sql server it would work with:
select col1,col2
from tbl
where col3=#myParam
How would it be typed in oracle or is it pl/sql?
I get the problem in the #myParam part.
Oracle SQL parameters are specified using :
SELECT col1, col2 FROM tbl WHERE col3=:myParam
You will have to be careful when specifying this as an OracleParameter though, as some libraries miss off the :, and some require it to bind correctly.