Create SQL Table from Random Array for WebService - sql

I have this little project that I'm working and I got to a part where I want to do something specific but can't seem to figure out a solution that works. Basically I've created a Web Service in Visual Studio that is used for a mobile app and we wanted to add a new piece of functionality that allows the retrieval of clients from multiple users. To do this I need to be able to pass an array of values to a WHERE IN clause, which I've been finding out is pretty much not possible but the other solutions I've read (that have no real example of the process) is to create a table of the Array values and then to a WHERE IN (SELECT * from TEMPTABLE) type of scenario.
So this is where I'm stuck, how do I go about passing an array of values to my SQL statement that will then create a temp table of those values (can range from 1 to 30 values) so then I can run other queries against those values. Most of the examples I've seen have the temp table hard coded with values, but I need to be able to pass an random amount of values for that table, so if it's easier to pass them as one single string with commas is fine or if they need to be passed in a specific format I can do that as well on the iOS side before calling the Web Service, I just can't seem to figure out how to do this dynamic type of table to move on to the next portion.
EDIT (Adding some information based on Comment)
So some background information on the system and purpose to help get a better understanding of what I'm doing. The Database is a SQL Server (2008) with already existing tables and data since what I'm working on is a Mobile Application port of an existing Web Application. Two tables that I can use for example are an Agent table (for employees) and a Customer Table (for their client) and so in some cases we have Teams that can view each other's clients. So normally if I wanted to pull a list of clients from a group of agents I would use the WHERE IN statement, but for the Web Service it would need to take that group as a Variable since the Rep may want to see only a few team members or all team members (this would be an option set in the iOS App).
I don't really have code that works, and most of what I have tried I know doesn't really work so I'll put some pseudo code to explain what I want to do. At first I thought I could just do this:
SELECT C.first_name + ' ' + C.last_name as [Client Name]
FROM dbo.agent as A INNER JOIN
dbo.contact as C ON C.agent_id = A.id
WHERE (A.id IN (#RepList))
In this example, #RepList would be a string passed from the iOS App to the Web Service that contained all the ID's that were selected within the Application. As I've looked into this, I can't have a variable in a WHERE IN statement but I've read that if I create a Temp Table with the variables then I could reference it like this:
CREATE TABLE #TempTable (TempID VARCHAR(MAX))
INSERT INTO #TempTable (TempID)
VALUES (#RepList)
SELECT C.first_name + ' ' + C.last_name AS [Client Name]
FROM dbo.agent AS A INNER JOIN
dbo.contact AS C ON A.id = C.agent_id
WHERE (A.id IN (SELECT #TempTable.* FROM #TempTable))
So again, #RepList would be the list of ID's that are passed from the iOS Application over to the Web Service that will run this query. This is where I'm stuck, since I can't seem to figure out how I can pass a list of values as a Variable into the TempTable. This may end up just being an issue in Visual Studio (since I've had issues with the SQL Query Designer and it not accepting certain things that end up working in the WebService) but I want to be sure this is the right format of doing so before moving forward. In the end, the WebService needs to be able to return a list of Clients based on however many Agents the person wants to see (this can range from just 1 agent to 20 agents).

Since you are on SQL Server 2008 and VS2010, probably the best solution is to use a Table-Valued parameter. This link (here) describes how to use them from a .Net client.
The basic idea is, your webservice code will call a procedure, function or parametized query on SQL Server, where one of the parameters is a table of values. Then the SQL code will use that table-valued parameter as its source matching table instead of a temporary table.

Related

Missing query data when using Sum

My database in Microsoft Access looks like this:
Every Client can have many assistants.
Every Assistant may have one client or no clients at all.
Assistant have a Nice field which is Boolean, indicating whether the Assistant is nice.
I need a query where I can get all of the clients, together with a boolean value indicating whether they have at least one nice assistant.
Currently this is the query I have:
This query is working as you can see: (apperantly 0 is false and -1 is true)
But here is the problem:
If there is a Client with no Assistants at all, it will not show up in the query.
I am wondering if there is a way to add all of the Clients with no Assistant to the query and their MinOfAS-Nice column will be 0. I will also accept any other creative way for example creating another query - but in the end I’m going to need a one query with all of the Clients data.
I need this data for a Report I'm going to create in Access.
Thanks in advance!
GitHub repo: https://github.com/orihpt/MissingRecordsInQueryAccessIssue
For your convinience here is the query as SQL query:
SELECT Client.[CL-ID], Client.[CL-Name], Min(Assistant.[AS-Nice]) AS [MinOfAS-Nice]
FROM Client INNER JOIN Assistant ON Client.[CL-ID] = Assistant.[AS-Client]
GROUP BY Client.[CL-ID], Client.[CL-Name]
ORDER BY Client.[CL-ID];
Use a Left Join:
SELECT Client.[CL-ID], Client.[CL-Name], Min(Nz(Assistant.[AS-Nice], 0)) AS [MinOfAS-Nice]
FROM Client LEFT JOIN Assistant ON Client.[CL-ID] = Assistant.[AS-Client]
GROUP BY Client.[CL-ID], Client.[CL-Name]
ORDER BY Client.[CL-ID];
Also: the Nz function is a default operator, means that on records without any linked records on the another table you will get 0 instead of Null.

SQL "LEFT JOIN" from multiple databases with a variable database name

I'm trying to query an existing SQL server which has a very different structure than I'm used to. This server manages Bill of Materials (BOMs)for a company. For some reason, this is set up so that each project has it's own database. There is a separate database ("project") to hold the basic information for each project.
My goal is to return a list of all projects which meet a condition in the "project" database AND contain a certain component in their BOM.
I can run a simple SELECT command on the "project" database to come up with all projects which meet the first condition. For example, lets say I'm returning (3x) projects which have an int value (87,89,93).
My challenge is that I now need to check the BOM of these (3x) projects to see if they meet my second condition. I essentially need to add a "WHERE" condition to the end of my search that looks like the following.
WHERE
(SELECT COUNT(item)
FROM [PROJECT_87].[dbo].[BOM]
WHERE item like '%ComparePN%') > 0
I'm stumped by figuring out how to change the name of the database "PROJECT_87" for each of the returned projects (87,89,93). I know I can use the SET + REPLACE commands to do the actual replacing of the '87' with the other project numbers. I don't know how to go through all 3 results and perform the SET + REPLACE command for each row to evaluate if the row should stick around.
Thoughts?

Adding New Fields via LEFT JOIN in Running ETL from SQL to MongoDB

I need to run an ETL to get data from a Sybase/SQL back-end into my MongoDB environment. We already have data from Sybase, but now there are a couple of additional fields we want to pull data in for. So with my familiarity being with Mongo (not so much Sybase), I'm trying to determine how I need to adjust our ETL to get this additional data.
The current SELECT statement looks like this:
`SELECT DISTINCT TOP 100 d.*, d10.code code10, d10.id_number as Code10ID FROM diagnosis d LEFT JOIN diagnosis_icd10 d10 on d.icd10_id = d10.id_number ORDER BY d.id_number`
Now, within the diagnosis_icd10 table that we're doing the LEFT JOIN on, there are now a couple of extra fields available.
So, my question is, do I need to explicitly include these additional fields here in the SELECT statement in order for them to be available in the ETL process? Or is this only the case if I want to rename the fields? What should this look like?
Yes you need to explicitly request them, as your current query only gets all columns from the diagnosis table (aliased to d via the d*).
This has nothing to do with Sybase though this is basic SQL so would be the same for most databases. You just add them to the select statement as in d10.column_name like the others.

Oracle Application Express SQL Query to show meaningful information

I am trying to write a query that 1) works and 2) shows meaningful information.
However, I can't seem to complete both scenarios. Both bits of code do work to a degree. My SQL query does work by showing all the useful information a user wants but when you click the edit button it doesn't link properly so it won't allow the user to update that row. The other shows only keys and rowid but when you click edit does show the information and allows it to be updated.
So as not to get another down-voted question, I have taken pictures of each scenario to show the problem, but, ultimately, I need to show meaningful information: an id or key isn't meaningful to the vast majority of users.
Here is my code
SELECT APPLICATIONS.APP_ID, APPLICATIONS.SRN, STUDENTS.SURNAME, STUDENTS.FORENAME, APP_STATUS.STATUS, METHODS.METHOD, JOBS.JOB_TITLE, APPLICATIONS.APP_DATE
FROM APPLICATIONS
JOIN STUDENTS
ON APPLICATIONS.SRN = STUDENTS.SRN
JOIN APP_STATUS
ON APPLICATIONS.STATUS_ID = APP_STATUS.STATUS_ID
JOIN METHODS
ON APPLICATIONS.METHOD_ID = METHODS.METHOD_ID
JOIN JOBS
ON APPLICATIONS.JOB_ID = JOBS.JOB_ID;
and here are the pictures of it in action
below is the code that does not show meaningful information but does work.
select "ROWID",
"APP_ID",
"SRN",
"STATUS_ID",
"METHOD_ID",
"JOB_ID",
"APP_DATE"
from "#OWNER#"."APPLICATIONS"
If i knew how to properly use rowid i am sure this is a simple feat but i dont so if i could get any help it would be useful
//edit
who ever renamed this to Application Expression why? what i am using is Apex Application Express it was relevant information that got changed to something wrong which might make it hard for someone with a similar problem to find later.
In the second, simple query, apex can determine which table (and record) you are trying to edit.
In the first query, with the joins, it can't tell which of the five tables in query you want to edit. You probably want to have the edit link pass the primary key of the row from APPLICATIONS to the child page. You would need to build into that page any logic (lists of values etc) that map lookup tables (such as status) to the values needed in the APPLICATIONS table.

SQL query / SQL Reporting Services

Been rattling my brain for a while and I could not get pass how to do the SQL query that will show the relationship/connections between my two tables.
I'm working on an IT equipment inventory program. I have two tables;
SELECT serial_number, model, ship_dat, status FROM items_list
SELECT item_serial, connected-to_serial FROM connections
All items like desktops, laptops, monitors, etc are on the items_list table. To track down the relationship/connections of the items, I created the connections table. IE, Monitor with serial_number=Screen#1 is connected to a Desktop with serial_number=Serial#1. It works ok with my Window Form application because I
used a datagridview control to list all devices simple SQL query.
However, when trying to show the relationship/connection on SQL Reports I've ran out of ideas how to do it. I'm aiming to get the report look like below or something along the lines. I just need to show the connections between the items.
Thank you
You should be able to do this with a table in SSRS if that is what you are using. The query you would need to drive the table of all related items would be:
SELECT item_serial, connected-to_serial, mainItem.*, connectedItem.*
FROM connections
INNER JOIN items_list mainItem ON connections.item_serial = items_list.serial_number
INNER JOIN items_list connectedItem ON connections.connected-to_serial = connectedItem.serial_number
You can of course tailor the SELECT statement to your needs, mainItem.* and connectedItem.* will not give you the most descriptive column names. Using column aliases (found under column_alias here) you can give a more descriptive name to each column.
From here you should be able to use a table and create a row group on the main item (either name or serial number) to get the type of look you are looking to achieve here. I believe the Report Wizard actually has most of the functionality you are looking for and should handle the bulk of this. You may have to move some of the cells around to get the look you are going for though.