I am working with two seperate SQL statements when user conducts a search on my winforms app.
A user can search based on an OrderNumber, OrderDate, ClaimNumber, ClaimDate, CustomerID, CustomerPhone
One SQL statement returns the OrderNumber (in case the user searched based on OrderDate), which is used to execute another SQL statement, which returns the details about the order.
The results are populated in a gridview.
Once the first statement is executed, I have a DataSource (DataSet). How can I append that datasource to display the fields from the second statement as well, keeping in mind, that the first statement could return multiple records, all of which will need to have the appropriate order details correctly linked.
Any ideas / suggestions?
SQLDataSet? Is this a custom structure? It's not a .net structure is it?
If you are referring to .net DataSet you can use the Merge method to merge 2 datasets.
Related
I am trying to see if there is an easy answer for this. I have done something similar using multiple pick dropdown parameters in SSRS but this appears to be different.
My scenario is this, so maybe there is an even better answer.
I have a production server that I do not want to make any changes to including temp tables or functions. The production server has a table of clients with about 1600 records. I have set up an SSIS package that will allow transfer of data from production to dev based on a clientid. So my sources would have a query similar to Select Field From Table Where ClientId = ?
This works fine. Now I want to load more than one client, based an data in the clients table. It may be Select ClientId From Clients where Field = A and returns multiple ClientIds.
I am able to populate a comma delimited list from an execute sql task to a SSIS variable, so it maybe 1,4,8.
If I change my source query to use ClientId in (?) I get a conversion error.
I have looked at many posts that advocate a temp table or a function which I want to avoid. Select IN using varchar string with comma delimited values
I have contemplated building the entire sql statement into a variable but this don't seem like the right path as I have many tables to query and transfer where using ClientId = ? works well without having to build each individual SQL statement to a variable.
Is there an easy fix I am missing? I will turn my research now to try to find out how I did this in SSRS but I thought that I should try a post here to see if someone has accomplished this before.
I appreciate any info on this, thank you.
EDIT: Key note is that the column on clients is on the dev server, so I cannot just use a select in the where clause as the column does not exist on the production server.
EDIT: I did not mention that I am specifically looking at OLEDB sources mapping a parameter to ? in the sql statement.
EDIT: Narrowing down on this but having trouble relating SSRS and SSIS functionality. In SSRS its called a multi-value parameter in the following link the key line is
WHERE Production.ProductInventory.ProductID IN (#ProductID)
https://msdn.microsoft.com/en-us/library/dn385719(v=sql.110).aspx
This one looks good as well
https://sqlblogcasts.com/blogs/simons/archive/2007/11/22/RS-HowTo---Pass-a-multivalue-parameter-to-a-query-using-IN.aspx
I will keep researching and thank you for the help so far.
I think this sums it up best
This functionality is limited to strictly using embedded SQL.
What SSRS does is transform your SQL column IN (#value) to column IN
(#selectedvalue1,#selectedvalue2) etc.
You need to forget anything you have about the other ways of passing
lists to SQL i.e. building strings etc. and make sure you declare the
data types are correct for the value of your parameter.
You do not need to use the Join(parameters!,",") trick UNLESS
you are passing the list to a stored procedure.
In which case you then need to use some function to turn the delimited
list into a rowset as you have done.
I hope that helps
The core question is if I can get the same functionality in SSIS as in SSRS. It reminds me of macro substitution..
If you dont want to create a function, you can use the following in your t-sql statement.
Declare #ClientIds nvarchar(50) = '123,456'; --<-- Comma delimited list of Client Ids
Select Field
From Table
Where ClientId IN (
SELECT CAST(RTRIM(LTRIM(Split.a.value('.', 'VARCHAR(100)'))) AS INT) ClientIDs
FROM (
SELECT Cast ('<X>'
+ Replace(#ClientIds, ',', '</X><X>')
+ '</X>' AS XML) AS Data
) AS t CROSS APPLY Data.nodes ('/X') AS Split(a)
)
I need to do join query to MS SQL Server 2014 DB based on a column name value. The same query runs when doing query directly to DB, but when doing query through Mule I'm getting error. The query looks something like this :
SELECT * FROM sch.emple JOIN sch.dept on sch.emple.empid = sch.dept.empid;
The above query work fine while doing query directly to MS SQL Server DB, but gives the following error through mulesoft.
Record cannot be mapped as it contains multiple columns with the same label. Define column aliases to solve this problem (java.lang.IllegalArgumentException). Message payload is of type: String
Request you to please help me out.
Specify columns list directly:
SELECT e.<col1>, e.<col2>, ...., d.<col1>,...
FROM sch.emple AS e
JOIN sch.dept AS d
ON e.empid = d.empid;
Remarks:
You could use aliases instead of schema.table_name
SELECT * in production code in 95% cases is bad practice
The column that has duplicate is empid(or more). You could add alias for it e.empid AS emple_empid and d.empid AS dept_empid or just specify e.empid once.
To avoid specifying all columns manually, you could drag and drop them from object explorer to query pane like Drag and Drop Column List into query window.
Second way is to use plugin like Redgate Prompt to expand SELECT *:
Image from: https://www.simple-talk.com/sql/sql-tools/sql-server-intellisense-vs.-red-gate-sql-prompt/
Addendum
But the same query works directly.
It works because you don't bind them. Please read carefully link I provided for SELECT * antipattern and especially:
Binding Problems
When you SELECT *, it's possible to retrieve two columns of the same name from two different tables. This can
often crash your data consumer. Imagine a query that joins two
tables, both of which contain a column called "ID". How would a
consumer know which was which? SELECT * can also confuse views (at
least in some versions SQL Server) when underlying table structures
change -- the view is not rebuilt, and the data which comes back can
be nonsense. And the worst part of it is that you can take care
to name your columns whatever you want, but the next guy who comes
along might have no way of knowing that he has to worry about adding a
column which will collide with your already-developed names.
But the same query works directly.
by Dave Markle
I have a SQL Server database with table containing 300.000 data rows. There is an Index on the Primary Key and another key. I am using the following query in my standalone WCF server to fetch the data using an SQLConnection and SQLDataReader.
SELECT * FROM Users WHERE UserTypeId = #UserTypeId ORDER BY Users.Id OFFSET
#OFFSET ROWS FETCH NEXT #NUMBER ROWS ONLY
The Data returned by the DataReader is pushed into my own Class/Model and than returned by the function of the WCF server.
The WPF Client connects to the server and starts the command and only wants 500 data rows. However the time needed for this task is about 3-4 seconds. (Not mentioning the time for all data...)
The returned List is then used as the DataContext for the WPF Datagrid.
My question is, what can I check or what might be wrong. If you need more Information,CodeSamples,etc. please let me know!
First, don't use select *, instead specify what fields you want from the table. Now you are getting data that you don't need, for example the UserTypeId field which you already know for all the records that you get.
Then you can create a covering index that contains UserTypeId and Id, and has any other fields that you want to return from the query as included fields. That way the database can run the query against the index alone, and doesn't have to read anything from the actual table.
I have the following Query:
SELECT *
FROM tbl_Muffins
WHERE OvenLoadId IN (
SELECT OvenLoadId
FROM tbl_Muffins
WHERE OrderId = ?
GROUP BY OvenLoadId
)
ORDER BY OvenLoadId, LocationNumber ASC
The idea of the query is that I want to specify an OrderId, and I want all Oven Loads that have items from that order in them, as well as ALL OTHER ITEMS in those loads, even though they could be from other orders.
The query works as expected, but I'm not able to use it for my Crystal Report I've written it for.
I have put this query into a User Defined Function, however I'm unable to use a call to that function as a table source in my Crystal Report. I am using Visual Studio 2003 (I know it is old, the client refuses to upgrade).
What am I doing wrong?
Thanks,
jnsohnumr
From version 9 of Crystal Reports onwards, it became possible to report from custom queries using Crystal's "Add Command" functionality.
There is a description of adding a parameter to a command query here.
I have a Report in SSRS 2008 that looks like so:
For the Sales Attendees and Other Attendees it may have more than one person that needs to go in that row. As you can see from the screenshot right now it just creates another row. How would I make it so they go into the same row instead of creating new rows?
EDIT: I have to use FetchXML. I'm working against CRM 2011.
Thanks!
As Brent D mentions, I would rather do this in the data source, but you can do the same thing with the RunningValue function and some custom code. (Another blog entry using the same approach.)
Write the custom code to take in additional strings and add them to the end of a stored string. It will need a second parameter, so you know when to clear out the stored string and start over for the next group.
Once you've got this working, you can use this in a group footer, and then hide your data rows.
You could try concatenating the names with a comma in the sql datasource by doing something like:
Select <main data columns, e.g. RSM/NAM,AppointmentDate,etc>
,stuff((select ','+SalesAttendee From tableB Where TableA.ID=TableB.ID For XML Path('')),1,1,'')
From TableA
See SQL Concatenation for reference.