SQL Server variables and direct values not returning same results - sql

I'm working on building a sql query to fetch some details from a table. And I am using a variable that is assigned with a value in where condition. However, when the value assigned to the variable is directly given to the where condition it returns a different result set. I cannot disclose the entire query here due to the nature of work but I hope someone could help me.
declare #filterParam varchar(255) = 'Y'
Select .......
from Table A
where
...
A.column!= #filterParam
This returns some number of rows but if i replace the variable with its actual value then 0 rows are returned
Select .......
from Table A
where
...
A.column!= 'Y'
0 rows is what the expected result is. But why is the SQL Server returning result when the value is assigned to a variable and used it against the where condition.
Thanks in advance.

Related

Dealing with multiple output results in UPSERT query [SQL]

I'm trying to do an update query in which a single row the table is updated and, if nothing has matched and updated, a new row is inserted. In each case, I need the query to return the ID of the inserted row.
The issue I'm having is this query is returning 2 separate results when the insert case is reached, one for each output (the first empty, the second containing the ID). I'm running this query using SQL Alchemy on python and I'm only able to see the first result, which is empty.
UPDATE [Rights]
SET accessLevel = :access_level
OUTPUT inserted.rightsID
WHERE principal = :principal and [function] = :function
IF ##ROWCOUNT = 0
INSERT INTO Rights(principal, [function], accessLevel)
OUTPUT inserted.rightsID
VALUES(:principal, :function, :access_level)
And I'm calling it like so:
inserted_right_id = session.execute(sql_rights_update, right).fetchall()
Can anyone recommend a way of changing the query so that I can still use the UPSERT method, but only receive one of the IDs? I was considering storing the OUTPUT values into a table and returning that, or wrapping the whole thing in a select but hopefully there's something more elegant out there.
Thanks a million.
Feeling quite dumb. I simply added a
IF EXISTS(SELECT * FROM Rights WHERE principal = :principal and [function] = :function)
UPDATE ...
ELSE
INSERT ...

Replace NULL in my Table with <SOME VALUE> in PostgreSQL

Upon searching ways to replace NULL values in my table with 0 on Stack Overflow, it appears that many threads I've found point to using the COALESCE function. E.g. postgresql return 0 if returned value is null
I understand that the COALESCE function "replaces" null values for your specific query; however, the table itself remains untouched. That is, if you queried the table again in a separate query without COALESCE, null values would still exist.
My question is, is there a way for me to replace NULL values permanently in my table with a specified value (like 0) so I don't have to COALESCE in every query? And as an extension to my question, is it considered bad practice to modify the original table instead of doing manipulations in queries?
You can just do an UPDATE:
UPDATE table SET col1 = 0 WHERE col1 IS NULL;
This should effectively update all matching records on your table
I understand you got the answer but you can also use in your further query nvl function. You can replace at the runtime the NULL values with 0 just to be sure your query is working as expected.
SELECT
id
,nvl(col1, 0)
FROM
...
It's not updating in the table but you are sure that all NULL values are displayed as 0 like you want. Maybe you forget to update.

Sybase Sql query not returning error

While doing a query with multiples tables and multiples variables in a Sybase SQL query, the results weren't what i was expecting.
My table is not complete and is not supposed to be, most of it can recieve NULL as a value (I know NULL is not a value but in let's consider it just for this query, this is not the focus of the question) and it's never going to be completed.
That known, my query is fairly simple, it asks for the user for a few filters that he doesn't need to fill it out, if he just wants a more "open search". While doing tests with this query i realized that one of those filters where not working, even if i filled it, it would still return values as if the user had never filled the filter. Upon closer expection i realized that the function called to make the query was missing that filter so i went and changed it.
SELECT * FROM
CONTROL C,
MARKET_CONTROL MC,
STORAGE_CONTROL SC
WHERE c.item_control = mc.item_control
AND c.item_code = isnull(#itemCode,item_code) #itemCode is one of the filters
AND c.item_baseline = isnull(#itemBaseline, item_baseline)
AND c.item_quantity = sc.item_quantity
AND c.item_storage = sc.item_storage
AND others, the list goes on.
While others are working, if i don't give the filter info, it ignores it and searches for himself, returning all possible values right? I mean, the ISNULL function can be translated as this:
if #itemBaseline IS NULL then
c.item_baseline = c.item_baseline
else
c.item_baseine = #itemBaseline
Right?
Well, i'm not trying to return null or anything, nor am i trying to make it searches for himself so i can return everything. But item_baseline can be added to the control table without any info o it (giving it the NULL value [Again, let's ignore it, just trying to make it easier to understand]). Well, the results of the query, however, instead of returning everything on itemBaseline it works like the user selected IS NOT NULL.
I'm not trying to return null in the query, all i want is to make it return every possibility.
PS: I'm using Sybase-based SQLdbx 3.12.
PS2: I've tried using alternatives to isnull function like coalesce but it still doesn't work.
It seems that your variables are not defined during execution of this query. I did small experiment on my side :
CREATE TABLE #temp
(
a INT NULL,
b INT NULL
)
INSERT #temp
SELECT 1, NULL
UNION
SELECT 2, 2
DECLARE #b INt
SELECT #b = 2
SELECT * FROM #temp
WHERE
b = isnull(#b,b)
Output is filtered properly, if you comment definition of #b ( --SELECT #b = 2) you will get entire table. GL.

What does 'null' mean when preceding a value in a SELECT statement?

Apologies if this has been asked before but I can't seem to find a relevant answer. It looks like it should be rather simple.
I'm looking at a store procedure (in Sql Server 2008) that includes a select statement similar to the following:
SELECT id, name, null revisedDate
FROM MyTable
What does 'null' mean when preceding the column 'revisedDate'? Does this mean make the returned column values null regardless? I thought it might be a way of giving it a default value of null, but then surely if there's no value then it would return null anyway.
It simply means that you are including a hardcoded NULL value in your result set. Further revisedDate is the name given to that column. For every row in the result set, this column will show NULL.
Assuming TSQL..
Im pretty sure that that will just return a column of nulls under the heading revisedDate. It wont bring back values form any column as in SQL syntax this is the equivelent of null as revisedDate
Hth
O

SQL Server Stored Procedure - Use Row Count in Select query

My stored procedure (SQL Server 2005) returns a dataset where one field depends, among other things, on the number of rows returned by the query. I can make a simplified first query that allows me to get ##ROWCOUNT but, in that case, the procedure returns the two sets, which is not what I want.
I tried putting the first query in a WITH statement but haven't found the syntax to extract the row count and put it in a variable that I could use in the second query. An alternative would be to get ##ROWCOUNT from the first query and tell the procedure to return only the result of the second query.
There are probably better ways to do that but my expertise in SQL is quite limited...
Thanks for any help!
Is this what you're looking for? If not, could you please describe your problem in more details (perhaps, with code snippets)
alter procedure ComplicatedStoredProcedure as
begin
declare #lastQueryRowCount int
-- Storing the number of rows returned by the first query into a variable.
select #lastQueryRowCount =
-- First resultset (not seen by caller).
(select count(*) from A where ID > 100)
-- Second resultset. This will be the actual result returned from the SP.
select * from B where SomeDependentField > #lastQueryRowCount
end