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

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?

Related

Using MS Access to obtain data across linked tables

I'm new to MS Access and am trying to speed up a data gathering process that is taking forever in Powershell. In Powershell I have 10 or so web API calls to get data and each comes back as an object with multiple properties (fields.) Each set of data has related fields to 1 or more of the other sets of data. Getting the data is very quick but piping an array of objects to where-object to select-object takes over an hour and there's really not that much data. Each object contains 500-1500 "records" and 5 to 10 "fields" so I thought why not export that data and use something that's intended to search through data to do the job? I exported each object as a separate .CSV file. So enter MS Access..
I imported each of the CSV's as a separate table (easy enough.) I'm going to simplify this down for this example to the following 3 tables:
[Tables]https://i.stack.imgur.com/UCH1F.jpg
Every table has fields that relate it over to other tables. Pretty much there's some sort of Id field in every table that is related to another Id field in a different table that I need to pull a field called "name" from. I'm trying to follow the bread crumbs from the Player name to it's Network name to it's Application name, to it's Layout name, etc... I want to build a query that I would eventually just be able to export as an Excel file. I also would prefer to just write out the SQL unless it's really easier to to understand the visual query builder. I'm looking to build a sheet with the following information:
Player's Name would include all names from the Players table and getting just that data makes sense to me. SELECT Name AS PlayerName FROM Players Everything else, not so much. I feel like this will end up being some mega query as I get deeper into related table after related table. In Excel, it would be straightforward using Vlookups across tabs but that doesn't seem to be the best approach. Given the info above, I'm trying to achieve the following output:
Result table
Any help with strategy and syntax greatly appreciated!
You're looking for the JOIN clause.
SELECT
Players.Name PlayerName, Networks.Name PlayerNetwork, Applications.Name ApplicationName
FROM
Players
LEFT OUTER JOIN
Networks
ON
Networks.ID = Players.NetworkId
LEFT OUTER JOIN
Applications
ON
Applications.Id = Players.ApplicationID

Copy one database table's contents into another on same database

I'm a bit of a newbie with the workings of phpmyadmin. I have a database and now there are 2 parts within it - the original tables jos_ and the same again but with a different prefix, say let's ****_ that will be the finished database.
This has come about because I am upgrading my Joomla 1.5 site to 2.5. I used a migration tool for the bulk of the new database but one particular piece of information did not transfer because the new database has a different structure.
I want to copy the entire contents of jos_content, attribs, keyref= across to ****_content, metadata, "xreference"."VALUE" if that makes sense. This will save manually typing in the information contained within 1000s of articles.
jos_content, attribs currently contains
show_title=
link_titles=
show_intro=
show_section=
link_section=
show_category=
link_category=
show_vote=
show_author=
show_create_date=
show_modify_date=
show_pdf_icon=
show_print_icon=
show_email_icon=
language=
keyref=41.126815,0.732623
readmore=
****_content, metadata currently contains
{"robots":"all","author":""}
but I want it to end up like this
{"robots":"","author":"","rights":"","xreference":"41.126815,0.732623","marker":""}
Could anyone tell me the SQL string that I would need to run to achieve this please?
If it makes any difference I have manually changed about 300 of these articles already and thought there must be a better way.
Edit: Being nervous of trying this I would like to try and find the exact syntax (if that's the right word) for the SQL Query to run.
The value I want to extract from the source table is just, and only, the numbers next to keyref= and I want them to turn up in the destination table prefixed by "xreference". - so it shows "xreference"."VALUE" with VALUE being the required numbers. There is also an entry - ,"marker":"" that is in the destination table so I guess the Query needs to produce that as well?
Sorry for labouring this but if I get it wrong, maybe by guessing what to put, I don't really have the knowledge to put it all right again....
Thanks.
Please Try it
insert into tableone(column1,column2) select column1,column2 from Tablesecond
if You have not Table another Daabase Then This query
select * into anyname_Table from tablesource

SQL Server: Remove substrings from field data by iterating through a table of city names

I have two databases, Database A and Database B.
Database A contains some data which needs to be placed in a table in Database B. However, before that can happen, some of that data must be “cleaned up” in the following way:
The table in Database A which contains the data to be placed in Database B has a field called “Desc.” Every now and then the users of the system put city names in with the data they enter into the “Desc” field. For example: a user may type in “Move furniture to new cubicle. New York. Add electric.”
Before that data can be imported into Database B the word “New York” needs to be removed from that data so that it only reads “Move furniture to new cubicle. Add electric.” However—and this is important—the original data in Database A must remain untouched. In other words, Database A’s data will still read “Move furniture to new cubicle. New York. Add electric,” while the data in Database B will read “Move furniture to new cubicle. Add electric.”
Database B contains a table which has a list of the city names which need to be removed from the “Desc” field data from Database A before being placed in Database B.
How do I construct a stored procedure or function which will grab the data from Database A, then iterate through the Cities table in Database B and if it finds a city name in the “Desc” field will remove it while keeping the rest of the information in that field thus creating a recordset which I can then use to populate the appropriate table in Database B?
I have tried several things but still haven’t cracked it. Yet I’m sure this is probably fairly easy. Any help is greatly appreciated!
Thanks.
EDIT:
The latest thing I have tried to solve this problem is this:
DECLARE #cityName VarChar(50)
While (Select COUNT(*) From ABCScanSQL.dbo.tblDiscardCitiesList) > 0
Begin
Select #cityName = ABCScanSQL.dbo.tblDiscardCitiesList.CityName FROM ABCScanSQL.dbo.tblDiscardCitiesList
SELECT JOB_NO, LTRIM(RTRIM(SUBSTRING(JOB_NO, (LEN(job_no) -2), 5))) AS LOCATION
,JOB_DESC, [Date_End] , REPLACE(Job_Desc,#cityName,' ') AS NoCity
FROM fmcs_tables.dbo.Jobt WHERE Job_No like '%loc%'
End
"Job_Desc" is the field which needs to have the city names removed.
This is a data quality issue. You can always make a copy of the [description] in Database A and call it [cleaned_desc].
One simple solution is to write a function that does the following.
1 - Read data from [tbl_remove_these_words]. These are the phrases you want removed.
2 - Compare the input - #var_description, to the rows in the table.
3 - Upon a match, replace with a empty string.
This solution depends upon a cleansing table that you maintain and update.
Run a update query that uses the input from [description] with a call to [fn_remove_these_words] and sets [cleaned_desc] to the output.
Another solution is to look at products like Melisa Data (DQ) product for SSIS or data quality services in the SQL server stack to give you a application frame work to solve the problem.

Create SQL Table from Random Array for WebService

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.

Use a Query from the Destination db to limit OLE DB Source task in SSIS 2008

All,
I have a package that I'm building as a data importer so I can copy sets of data from my production environment and develop on another instance.
I have two tables that contain header and detail rows for service tickets. Those service tickets are tied back to orders.
I am pulling the service tickets from a certain time window, however, the originating orders fall outside of the date range that I'm pulling for the tickets.
I want to be able to take the following steps in an SSIS package:
Import the header and detail rows within the given date range from prod to dev
Select the relevant order numbers from dev tables
Use the list of order numbers to import only the relevant orders from prod
I poked through other answers and couldn't find answers that addressed this directly, so I apologize if there is an answer out there and I missed it. I may not have been asking the question correctly. I'm assuming that I would need to pull those order numbers into a temp table or variable in order to apply them as a filter.
As I write this, it just crossed my mind to use a join on the source system with the ticket to order tables and still use the date range to limit, but I'm still posting the question to see if anyone has dealt with this before.
Your steps are already fairly clear, are you asking how to actually implement them? It looks like you can do all three steps by using SELECT statements in your data sources:
Build a SELECT statement dynamically with the correct dates to use in your data source. The dates could be programmatically generated in a script task, or saved in a database table and populated into variables. Then you copy the data across to the dev system.
Run a SELECT statement in the dev system that returns the order numbers, and copy the results to a table in the prod database.
Run a SELECT statement in the prod database that joins on the table from step 2 and copy the results back to dev.
An alternative to the table in steps 2 and 3 would be a lookup transformation, but if you have a large number of rows then using a table will probably be faster.