how to pass this information to SQL Server 2008? - sql-server

Please visit http://www.stumbleupon.com/
It asks you what interests you.
I would have a similar thing on my website except I would need to have 4 of such non-identical blocks. When a user has not selected anything in a section, it means s/he has selected all of them. 1 of these blocks would have around 10 options while the other 3 would have 2-3 options each.
How do I pass what all a user has selected to SQL Server 2008? Please note I would have 4 sets of multiple params, and these 4 sets are basically 4 different columns in the row all containing integer id's. If and only if one param per section was passed, I would have done something like
select * from table where column1 = #param1, column2 = #param2 and so on.
but that is not the case, because a user could pass multiple values for #param1 and so on.
I can get this done, but I want to know the most efficient way.
If my post is not clear, please let me know.
EDIT:
Basically, if I am storing articles in the database, I have 4 columns in the row, each integer. And I want to query these articles based on these 4 columns, but the catch is to pass the multiple values for these columns NOT just one value per column.

If your client language supports it, you should look into table value parameters:
http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters
if you can't use those, look into one on Arrays and Lists in TSQL:
http://www.sommarskog.se/arrays-in-sql.html
this is a very comprehensive list of dynamic search conditions in TSQL:
http://www.sommarskog.se/dyn-search.html
this may also help:
Sane/fast method to pass variable parameter lists to SqlServer2008 stored procedure

Try dumping all parameter values into the XML, then pass it to SQL Server, then parse it in the query.

Won't such a query work in this case?
SELECT *
FROM TABLE
WHERE COLUMN1 IN ( #param11, #param12, #param13 )
AND COLUMN2 IN ( #param21, #param22, #param23 )
and so on.

Related

How to match a substring exactly in a string in SQL server?

I have a column workId in my table which has values like :
W1/2009/12345, G2/2018/2345
Now a user want to get this particular id G2/2018/2345. I am using like operator in my query as below:
select * from u_table as s where s.workId like '%2345%' .
It is giving me both above mentioned workids. I tried following query:
select * from u_table as s where s.workId like '%2345%' and s.workId not like '_2345'
This query also giving me same result.
If anyone please provide me with the correct query. Thanks!
Why not use the existing delimiters to match with your criteria?
select *
from u_table
where concat('/', workId, '/') like concat('%/', '2345', '/%');
Ideally of course your 3 separate values would be 3 separate columns; delimiting multiple values in a single column goes against first-normal form and prevents the optimizer from performing an efficient index seek, forcing a scan of all rows every time, hurting performance and concurrency.

How to add 2 data sets in SSRS

I have 2 data sets that have the common column VNO. Now I want to get the report in ssrs like
VDate, Count('') from B for Vdate, Count('') from A for Vdate
Can anyone help me with the way, I tried the lookup but not able to get this output.
Note: I cannot join these two tables in SQL as these 2 tables are on separate instances and they are not connected and don't have permission to do so.
You need to use LOOKUPSET() . This returns an array which you can then check the length of like this
=LookupSet(Fields!InvoiceDate.Value, Fields!VDate.Value, Fields!VNO.Value, "DataSetB").Length
The 3rd argument is the returned value but as you are only getting a count, it does not matter what we return there. DataSetB is the case sensitive name of your second dataset.

Multi value parameter not working in SSRS report

I have a SSRS report. there is a long SQL query on the main query, in the last SELECT I want to filter the results with WHERE expression, the filter should be with a multi value parameter.
I set the parameter in this way:
Create a new Dataset with a query.
Add a new parameter to the Parameters folder (with name NewParam).
Check the "Allow multiple values" checkbox.
Add the parameter to the "Main Query" and set the value with this expression:
=Join(Parameters!NewParam.Value,",")
At the end of the Main Query I filter the results:
select *
from #FinalStatusTbl
where Test_Number in (#NewParam)
order by Priority
The problem is:
On the report when I choose one value from the list I got expected results, but If I choose multi values the results are empty (not got an error.)
Do you have any idea why?
(When I try this: where Test_Number in ('Test 1', 'Test 2') it works well).
When you create a dataset with a sql query, multi valued parameters work with the in(#ParamName) without any changes.
Replace your =Join(Parameters!NewParam.Value,",") with just =Parameters!NewParam.Value and you should be fine.
That said, the reason you see people using that join expression is because sometimes your query will slow down considerably if your parameter has a lot of potential selections and you data is reasonably large. What is done here is to combine the join expression with a string splitting function in the dataset that converts the resulting Value1,Value2,Value3 string value in a table that can be used in the query via inner join.
This is also a requirement if passing multiple values as a parameter to a stored procedure, as you can't use the in(#ParamName) syntax.
You could try taking the parameter out of the where clause and use the parameter in the filters section of the dataset properties.
This will effectively shift the filtering from the SQL to SSRS.
What you need to do is split your string in the database. What is being passed to your query is 'Test 1, Test 2' as a complete string, NOT 'Test 1' and 'Test 2'. This is why a single value works, and multiple values do not.
Here is a really good link on how to split strings, in preparation for your scenario. The function I most often use is the CTE example, which returns a table of my split strings. Then I change my SQL query to use IN on the returned table.
In your example, you will want to write WHERE Test_Number IN (SELECT Item FROM dbo.ufn_SplitStrings(#NewParam) , where ufn_SplitString is the function you create from the link previously mentioned.
This is what I did and it works well to me. You can try it also.
=sum(if(Fields!Business_Code.Value = "PH"
and (Fields!Vendor_Code.Value = "5563"
and Fields!Vendor_Code.Value = "5564"
and Fields!Vendor_Code.Value = "5565"
and Fields!Vendor_Code.Value = "5551")
, Fields!TDY_Ordered_Value.Value , nothing ))

Constraining columns with each other in SSRS

I'm learning MSBI tools and came across a requirement in SSRS wherein we need to constrain report parameter 2 with parameter 1.
i.e. if I have parameters setup on [AdventureWorks2008R2].[HumanResources].[Department] table and if I select "Group Name" (Parameter 1) as "Manufacturing" then the "Name" (Parameter 2) drop down should populate only relevant values("Production" and "Production Control") specific to selected parameter 1 value ("Manufacturing")
I have been exposed to OBIEE tool and know that under this tool we have a "constrain" option under Prompts to achieve the same.
I could come up with the following solution under SSRS:
Created two datasets for each of the parameters with below queries.
Parameter 1 (GroupName)
SELECT DISTINCT GroupName
FROM HumanResources.Department
Parameter 2 (DeptName)
SELECT Name
FROM HumanResources.Department
WHERE (GroupName = #GroupName)
This worked. However if there are 5 such parameters which are to be constrained then with this solution I would have to create 5 such datasets and accordingly build the WHERE clause for each parameter.
Is there a better solution to achieve the same (there should be..)
I'm new to SSRS and therefore would appreciate any pointers here.
Thanks!
What you are talking about is called "Cascading Parameters" and they are implemented exactly as you did. You can read about them here:
Adding Cascading Parameters (SSRS)
So if you need to implement 5 of such parameters you have to write 5 queries for 5 datasets each one containing WHERE referencing parameter defined in previous dataset

Possible to replace digits in T-SQL

SQL Server 2008 (but have access to higher versions too)
I'm getting a string from another database on the same server. Using the below code i get some data and replace the content
INSERT INTO [DestinationDatabase].[DBO].[Table](ID, XML)
(SELECT ID, REPLACE(XML,'ReferenceID="1234"','PropertyID="2468"')
FROM [SourceDatabase].[DBO].[Customers]
This works as expected but every record has a different ReferenceID so is there a way to remove the current ReferenceID value as in the 4 digits (theres around 1000 records with different values) and replace it with another 4 digit value?
I will get the replacement value from another procedure but at this stage i need to know if it possible to find and strip the 4 digits and replace them.
If you want to use the replace function you can do it like that
REPLACE(XML,'ReferenceID="'+cast(table.field as nvarchar)+'"','ReferenceID="2468"')
REPLACE(XML,'ReferenceID="'+cast(table.field as nvarchar)+'"','ReferenceID="'+cast(table.another_field as nvarchar)+'"')
You can use xml function to do so but it seems like your XML column is not xml data type. is that correct.

Resources