SQL Server accessing rows - sql-server

I have a database where one table is as follows:
Owner Type | Owner Name
---------------------------------
Testing | abc, def, xyz
Testing2 | ppp
Testing3 | jkl, mno, pqr
In the SQL Report (SSRS), I would like to display something like this:
Testing owners = abc, def, xyz
Testing2 owners = ppp
Testing3 owners = jkl,m mno, pqr
How do I build a report in this way, using SQL Server Report Builder?
I understand that a simply query would work using the regular SQL Query mode. However, the difficulty I am facing is that there is only a single table cell where I need to enter the expression to be evaluated (there are more things in the table and the report, this is only a subset). I'm not sure how I can get that done.

There are at least 2 ways to do this
I would recommend doing the catenation in the report itself. Assuming you have already used the wizard to create the dataset, and the wizard has both raw columns in your report already, right click on the OwnerType detail row text box and select Expression (fx). You can then project the required display, e.g.:
=Fields!Owner_Type.Value + " owners = " + Fields!Owner_Name.Value
And then change the column name and delete the second column entirely.
The other way to do this is by projecting this directly in a Sql Query, e.g. by providing the following query by using the configure option on the DataSource:
select Owner_Type + ' owners = ' + Owner_Name as OwnerRow
from [dbo].[Own];
(you could also create a proc or view in SqlServer to do this projection and then bind the report to the proc / view).

If you
SELECT [Owner Type], [Owner Name] FROM Table
You can group by [Owner type] in a table/matrix in your SSRS report and get something that looks like
Testing
Testing2
Testing3
Then you can add what you need after that on a new column f.ex. But grouping is the way to display all [Owner type].
Edit: Suggestion to filter:

Related

SSRS Report Manager: Linked report behaving strangely but SQL queries work fine

I have created a report (call it Primary) and a drillthrough (call it Secondary) in Report Builder. Each of these has a SQL statement.
When executed in SQL Server Management Studio, SQL statements work as expected.
However, when the Primary.rdl and Secondary.rdl are uploaded to Report Manager (the web interface in Internet Explorer), they do not generate the correct data when run.
Because of this, I think the problem is not the SQL statements. I think it's something to do with the Report Manager.
Primary SQL statement:
This statement grabs a bunch of user data from multiple tables and checks if their passwords are acceptable. It populates a list of users whose passwords failed the check.
This is pseudocode so pardon inconsistencies in var names
with details as (
select u.userid
, u.password
, u.firstname
, u.lastname
, u.userdescription
, u.status
, u.lastlog
, dbo.IsPassswordAcceptable(u.userid, u.password) as passStatus
from masterListOfUsers as u
)
select d.*, p.datavalue
from details as d
left join passwordDetailList as p
on p.keyvalue = d.passStatus
and p.datatype = 'ERRORMESSAGE'
where d.passStatus <> 1
and d.passStatus <> -5
and d.status = (#USERSTATUS) -- only user ids in use
;
Secondary SQL statement:
This statement is a drillthrough. The person running the report can click on a userID in the above list. A drillthrough is performed where the contact information for that userID is populated.
This is pseudocode so pardon inconsistencies in var names
SELECT
m.userid
, c.address
, c.city
, c.state
, c.zip
, c.cphone
FROM userMasterList AS m
left join userDetailList AS d
ON d.userid = m.userid
left join anotherList as e on d.fullkey = e.fullkey
left join yetAnotherList AS c
WHERE m.userid = #USERID;
Expected result:
When the user runs the Primary, a list of users with bad passwords is populated. Each user's userID can be clicked on, which triggers the Secondary to populate the location/contact info associated with that userID.
Actual result:
On userID click, the Secondary fails to populate any location/contact info associated with the userID. This occurs only sometimes. Other times, it works fine.
I made a list of these "empty" userIDs and ran the Secondary's SQL statement in Management Studio, and it populates all the expected location/contact info.
Solutions I've tried:
I'm absolutely stumped. I've triple-checked the SQL statements and tested them in Management Studio. I've re-uploaded both .rdl files to Report Manager. I've reassigned the Secondary to the Primary via the "Create Linked Report" option in Report Manager AND ALSO in Report Builder's Action > Go To Report option.
What else can I do?
This is not really an answer as such, but a list of things I would work thru in the same situation.
Run SQL Profiler to trace your report session and make sure the query being executed is what you expect. Depending on how parameters are passed to the SQL statements, SSRS will not always do things quite the way you expected.
Check if you can repeat the issue by just running the drill thru report on it's own (not via the primary report)
Determine if the issue is consistent with specific userids? i.e. does user A always fail and User B always work? If the issue is consistent, the issue is most likely to be data related. Check for special characters in the fields that appear to be blank such as chr(13)/chr(10), they may just be forcing the 'real' content onto a new line inside the textbox.
Add some debug info to your report to help identify the issue such as:
a. Edit the dataset query to add some more info from dataset itself SELECT .... , c.addrees, len(c.address) as AddresLen from .... You can add this to a copy of your report
b. Add another textbox that does the same thing but directly in SSRS (e.g. expression would be something like =LEN(Fields!address.Value)). You then have two numbers to compare against what you can see. If the LEN textbox says 20 but the address field appears blank, then special characters could be the issue.
After hours of tinkering, the problem ended up being that the userID was being trimmed of all leading and trailing whitespace by some of the query tools but not by the SQL statements themselves. So when the final report is run in Report Manager, the data is queried with superfluous whitespace, resulting in no data being found.
This issue is resolved when the data points are trimmed.
The fixed Secondary SQL statement:
This is pseudocode so pardon inconsistencies in var names
SELECT
rtrim(m.userid) as userid
, rtrim(c.address) as address
, rtrim(c.city) as city
, rtrim(c.state) as state
, rtrim(c.zip) as zip
, rtrim(c.phone) as phone
FROM userMasterList AS m
left join userDetailList AS d
ON d.userid = m.userid
left join anotherList as e on d.fullkey = e.fullkey
left join yetAnotherList AS c
WHERE ltrim(rtrim(m.userid)) = ltrim(rtrim(#USERID));

How to arrange or format the table results in Oracle SQL Plus

I started learning Oracle and I've been gone through different types of examples. But from the beginning I have an issue that how can we arrange the retrieve table in well formatted view. I am using SQL Plus.
I attached a image of the way I get the output. I want the output table in a proper table view ( Organized columns and rows ).
SELECT DISTINCT c.name,
i.company.company AS stock_name,
i.company.current_price AS cur_price,
i.company.last_dividend AS last_devident,
i.company.earning_pershare AS eps
FROM client_table_c c, TABLE (c.investments) i
You can do the following to have the output in a single line and in a more formatted way
SET LINESIZE 32000;
SET PAGESIZE 40000;
For more formatting options, see Oracle Documentation
In SQL*Plus, there's the COLUMN command (can be abbreviated to COL) which allows you to format columns. For example:
SQL> create table test
2 (id number,
3 name varchar2(50));
Table created.
SQL> insert into test values (1, 'Littlefoot');
1 row created.
SQL> select * From test;
ID NAME
---------- ---------------------------------------
1 Littlefoot
SQL> col name format a15
SQL> select * From test;
ID NAME
---------- ---------------
1 Littlefoot
SQL>
See? NAME column is now shortened.
For much more info, have a look at Formatting SQL Plus reports (feel free to read the whole SQL Plus document).
P.S. I'm glad you use SQL Plus. It requires you to actually know what you're doing. GUI tools allow you to click here & click there, not really knowing what's behind that click and what result you can expect. However, once you get used to use Oracle, switch to - for example - SQL Developer (a free GUI tool by Oracle) which displays results of your queries in a more appropriate manner, without the need to manually adjust the output in order to make it readable).

Turning a multi-value parameter into a temp table in SQL Server Business Intelligence Development Studio

I want to create a report in MS SQL Server BIDS (SSMS and Visual Studio). The user would enter a list of email addresses as a parameter. So #pEmails would be 'foo#bluh.com', 'bar#meh.org', etc. These email addresses may or may not be in a table.
I can simply do:
and Table.Email in (#pEmails)
and that works, except I need to return the email address if it's NOT found as well. So the results would be something like:
|email |found in table|
|------------|--------------|
|foo#bluh.com| Y |
|bar#meh.org | N |
I was thinking I could take the list of values entered as the #pEmails parameter and create a temp table with them, which I could then left join with, but my attempts to do so have not worked out.
declare #pEmails table (EmailAddress varchar(255));
insert into #pEmails values (#ReportParameter1);
select
*
from
#pEmails
The above works if only a single value is put into #ReportParameter1, but not if multiples are in it.
I am using SQL Server 2008. Any suggestions on how best to proceed?
As has been stated, you need some kind of split function, for analysis on the performance of various methods Split strings the right way – or the next best way is an excellent read. Once you have your function, you then need to define your query parameter as a string, rather than a table:
So your query would actually become:
DECLARE #pEmails TABLE (EmailAddress varchar(255));
INSERT #pEmails (EmailAddress)
SELECT Value
FROM dbo.Split(#pEmallString);
Then go to your dataset properties, and instead of passing the multivalue parameter #pEmails to the dataset, instead create a new one #pEmailString, and set the value as an expression, which should be:
=Join(Parameters!pEmails.Value, ",")
This turns your multivalue parameter into a single comma delimited string. It seems pretty backwards that you need to convert it to a delimited string, only to then split it in SQL, unfortunately I don't know of a better way.
Here are some learnings on this topic (standing on the shoulders of the information elsewhere in this thread).
Set a parameter (select 'multiple values' checkbox):
InputList
Establish dataset query:
SELECT *
INTO #InputTemp
FROM STRING_SPLIT(#InputListJoin, ',')
SELECT value as ValueName
FROM #InputTemp T2
WHERE NOT EXISTS (
SELECT MyValue
FROM MyTable T1
WHERE T1.MyValue = T2.value
)
Establish dataset parameters:
Name: #InputList | Value: [#InputList]
Name: #InputListJoin | Value(expression): =Join(Parameters!InputList.Value,",")
The element names can be changed as needed.
Somewhat on topic, other details that might be helpful:
[#InputList.IsMultiValue] --> true/false whether your parameter is multi-value (not whether there are multiple values)
[#InputList.Count] --> count of items in input list (excludes blank lines)
=Parameters!InputList.Value(2) --> return third value from list (counting from zero)

Add header title above the query result in SQL Server

How can I add heading title for each query?
I want to display it as a report with a heading above the query result of the table.
Something like this
HEADING
_____________________________
Alexis M. Smith | PHP 140,500
Johnny K. Black | PHP 50,000
James P. Blonde | PHP 30,000
I am using sql server management 2012 Studio and vb.net 2010
I don't have Business Intelligence to create a report from the sql server to vb.net 2010
You may can't add header in your query result in SQL-Server and show the result in your application..
But if you are asking to show the result in SQL-Server only, then this might help you..
--You have to add one select query before your actual query like this.
select '' as 'Your Heading Here' where 1!=1
select * from your_table
Select b.a AS Heading
FROM
( Select Concat('Mr. Xyz', ' | ', 'PHP' , ' | ' , '50000') AS a
) B
This is the closest I could think.

SQL Server : How to display all extended event tables

When I run my query
SELECT * FROM sys.sysobjects WHERE xtype = 'U'
I find there are 2 more tables added trace_xe_action_map and trace_xe_event_map, I realize then that these two siblings come from extended events but I remember inserting them into my database. Yet I don't know to which xtype they are both belonging to. Because if I run the above query in SQL Server Management Studio; I won't be able to find them. What query should I make to display them in SQL Server Management Studio?
Source1
trace_xe_action_map (Transact-SQL)
Contains one row for each Extended Events action that is mapped to a SQL Trace column ID. This table is stored in the master database, in the sys schema.
Applies to: SQL Server 2012+.
Source 2
trace_xe_event_map (Transact-SQL)
Contains one row for each Extended Events event that is mapped to a SQL Trace event class. This table is stored in the master database, in the sys schema.
Applies to: SQL Server 2012+.
You can use the following query to identify the Extended Events events that are equivalent to the SQL Trace event classes:
SELECT te.name, xe.package_name, xe.xe_event_name
FROM sys.trace_events AS te
LEFT JOIN sys.trace_xe_event_map AS xe
ON te.trace_event_id = xe.trace_event_id
WHERE xe.trace_event_id IS NOT NULL
Not all event classes have equivalent Extended Events events. You can use the following query to list the event classes that do not have an Extended Events equivalent:
SELECT te.trace_event_id, te.name
FROM sys.trace_events AS te
LEFT JOIN sys.trace_xe_event_map AS xe
ON te.trace_event_id = xe.trace_event_id
WHERE xe.trace_event_id IS NULL
By executing this query:
SELECT *
FROM sys.all_objects -- also FROM sys.system_objects
WHERE name like 'trace_xe_%'
You will have a result like this:
name | object_id | type | type_desc
--------------------+------------+------+------------
trace_xe_action_map | -463397375 | U | USER_TABLE
trace_xe_event_map | -319884821 | U | USER_TABLE

Resources