SQL Single Value or All - sql-server

For report purposes in SSRS I need an SQL query for one parameter which will select only one value or all values (not two or more). It's a single value parameter.
Well, my query should looks like this, but it didn't work:
SELECT ft.id, ft.name
FROM fundtable ft
UNION ALL
SELECT '11111111-1111-1111-1111-111111111111','All'
DECLARE #funds NVARCHAR(MAX)
SELECT #funds = COALESCE(CAST(entity_id AS NVARCHAR(255)),',')
FROM epev_conduit
SELECT #funds

If you are using this query for a dataset that your parameter is using for the Available Values, you don't need to put in into a string. Just use:
select ft.id, ft.name
from fundtable ft
union all
Select '11111111-1111-1111-1111-111111111111','All'
The use your Name field for the Label and your ID for the Value. You can Specify a Default value expression of
="11111111-1111-1111-1111-111111111111"
if you want the default to be all.

This should do the trick.
SELECT *
FROM dbo.YourTable
WHERE #id = id
OR #id = '11111111-1111-1111-1111-111111111111'

Related

SQL Query Results to Local Variable without unique identifier

I'm relatively new to SQL and I'm trying to write a query that will assign the result of multiple rows into a local variable
DECLARE #x VARCHAR(MAX)
SET #x= (SELECT someCol
FROM table)
--Does important stuff to the #x variable
SELECT #x
During my research I realized that this won't work because the subquery can only return one value and my query will return multiple results. However I can not do something like this:
DECLARE #x VARCHAR(MAX)
SET #x= (SELECT someCol
FROM table
where id= 'uniqueIdentifier')
--Does important stuff to the #x variable
SELECT #x
The reason I can't use a where clause is that I need to do this for the entire table and not just one row. Any ideas?
EDIT: I realized my question was too broad so I'll try to reformat the code to give some context
SELECT col_ID, col_Definition
FROM myTable
If I were to run this query col_Definition would return a large varchar which holds a lot of information such as the primary key of another table that I'm trying to obtain. Lets say for example I did:
DECLARE #x VARCHAR(MAX)
SET #x= (SELECT col_Definition
FROM myTable
WHERE col_ID = 1)
--Function to do the filtering of the varchar variable that works as expected
SELECT #x as [Pk of another table] --returns filtered col_Definition
This will work as expected because it returns a single row. However, I would like to be able to run this query so that it will return the filtered varchar for every single row in the "myTable" table.
If I understand correctly, you store a PK embedded in a string that you want to eventually get out and use to join to that table. I would think putting the group of records you want to work with into a temp table and then applying some logic to that varchar column to get the PK. That logic is best as set based, but if you really want row by row, use a scalar function rather than a variable and apply it to the temp table:
select pk_column, dbo.scalarfunction(pk_column) as RowByRowWithFunction, substring(pk_column,5,10) as SetBasedMuchFaster
from #tempTable.
You need to define what the 'uniqueIdentifier' is first.
Not sure about using a subquery and grabbing the result and executing another query with that result unless you do an INNER JOIN of some sort or if using python or another language to process the data then use:
("SELECT someCol
FROM table
where id='%s'" % Id)

select value into variable and alias in SQL Server

I'd like to simultaneously do two things in an MSSQL query:
select a field's value into a variable
select #myvar = colName from tableName
alias my column
select colName as [myCol] from tableName
I've tried these things:
Attempted Syntax select #myvar = colName as [myCol] from tableName
Attempted Syntax select #myvar = (colName as [myCol]) from tableName
Attempted Syntax select (#myvar = colName) as [myCol] from tableName
checked select statement syntax: https://msdn.microsoft.com/en-us/library/ms176104.aspx
If this is possible, how can it be accomplished?
A select can either assign values to variables or return column values, but not both.
In some cases, e.g. when using select to provide data to an insert, an output clause may be useful. A tool well worth having in your pocket as it provides access to identity values from insert and both before and after values when used with update.

MSSQL - WHERE LIKE a part of a string

if i hava a variable
declare #tag nvarchar(max) = '1570,20342'
and I wanna select a data ,her field "tag" value is
"1443,1570,3245,20342"
this value is string, and was combined by "1570" and "20342",
how to select that data out ?
Use a subquery to get the string stored in the variable:
Example:
DECLARE #tag NVARCHAR(MAX)
SET #tag = '1570'
SELECT *
FROM TABLE
WHERE COLUMN LIKE (SELECT '%'+#tag+'%')
You won't be able to search for both values in a column without splitting them first. You could do this by using two variables, like:
DECLARE #tag1 NVARCHAR(MAX)
SET #tag1 = '1570'
DECLARE #tag2 NVARCHAR(MAX)
SET #tag2 = '20342'
SELECT *
FROM TABLE
WHERE COLUMN LIKE (SELECT '%'+#tag1+'%')
OR COLUMN LIKE (SELECT '%'+#tag2+'%')
Although this is a quick workaround for this situation, it does not scale well and the only way to fix this would be to normalize your tables, stop using more values separated by commas on one row, instead use one value per row.

Is there such thing as a conditional operator in SQL Server?

I have the below SQL code snippet and I want to select sales from all customers by manipulating the value of the #Customer parameter. Is there any way to do this in SQL Server 2008 R2? I've tried setting #Customer = '%' but obviously that didn't work since there is no LIKE operator.
I only want to manipulate the parameter because there will be other times where I will
need to select only a single customer. Is there such a thing as using an IF/ELSE in the WHERE clause?
DECLARE #Customer varchar(5) = ''
SELECT *
FROM SalesData
WHERE Customer=#Customer
Is there such a thing as using an IF/ELSE in the WHERE clause
Absolutely, there is a CASE expression - not only in the WHERE clause, but also in other parts of your query. However, a more common approach is to use logical expressions in the WHERE clause that force SQL Server to take one condition or the other depending on the parameter setting.
my condition would be if #Customer = '' then select all
If you would like to select all customers when the parameter is set to empty, or select all customers where the parameter is not set, you can do this:
SELECT *
FROM SalesData
WHERE #Customer = ''
OR Customer = #Customer
If #Customer is set to '', the first clause of the OR expression will be TRUE for all rows, so all customers would be returned. However, when #Customer is non-empty, the second part of the expression would be evaluated.
Not quite sure how you need to perform search
But can try something like below
SELECT *
FROM SalesData
WHERE Customer like '%' + #Customer + '%'
Or
SELECT *
FROM SalesData
WHERE (LEN(#Customer)>0 AND Customer =#Customer)
OR (LEN(#Customer)=0)
In here I havent trim assuming you always pass ''. But you can always use LEN(LTRIM(RTRIM(#Customer)))
If you default #Customer to NULL rather than '' then you could just have
WHERE Customer = ISNULL(#Customer, Customer)

compare file names from one query against another query sql

I have a situation where I am suppose to compare a document name from one query against query.
I have a table called tbl_doc which consists document_name column.
Another table called tbl_content which consists content_name.
And I have two select queries like
select document_name from tbl_doc
select content_name from tbl_content.
SO I need to compare document_name against content_name if they are equal then need to do a insert operation.
Thanks
You probably mean to check if a certain value exists...
IF EXISTS(
SELECT 1 FROM [tbl_doc]
JOIN [tbl_content] ON [tbl_doc].[document_name] = [tbl_content].[content_name]
WHERE [tbl_doc].[document_name] = #Name )
BEGIN
INSERT INTO [...]
(
...
)
SELECT
...
END
select document_name,content_name
from tbl_doc
join tbl_content on document_name=content_name

Resources