Calling Search Parameters in SQL for multiple SQL Scripts. - sql

Is it possible to create a variable like call to be used holding the search parameters for a SQL "Select" call?. I have multiple SELECT SQL queries that use the same parameters for EXCLUDING certain data. That would be in the "NOTLIKE" portion of the code.
Can you have one variable in the SQL code that can be called in multiple SQL scripts is my question.
This will be for MS SQL Server Management Studio v17.8.1 and MS SQL Server 2008.
SELECT distinct DB.[ReportNumber]
,DB.[StatusText]
,DB.[ProjActStageName]
,DB.[ProjectOwnerTypeName]
,DB.[ProjTypeName]
,DB.[ProjWorkTypeName]
FROM [Database]
WHERE DB.ReportNumber = DP.ReportNumber
AND DB.ProjTypeName IN ('Custom House', 'Spec Houses')
AND DB.ProjectName NOT LIKE '%Repair%'
AND DB.ProjectName NOT LIKE '%Replace%'
AND DB.ProjectName NOT LIKE '%Abatement%'
ORDER BY DB.ReportNumber
TO -->
SELECT distinct DB.[ReportNumber]
,DB.[StatusText]
,DB.[ProjActStageName]
,DB.[ProjectOwnerTypeName]
,DB.[ProjTypeName]
,DB.[ProjWorkTypeName]
FROM [Database]
WHERE DB.ReportNumber = DP.ReportNumber
AND DB.ProjTypeName IN ('Custom House', 'Spec Houses')
(Variable excluding certain data)
ORDER BY DB.ReportNumber"

You might want to read this thread for some options on how to approach your problem: SQL Query with NOT LIKE IN

Related

Passing Array of Values into Inner SQL Server Select Statement in Node Project

In my Node batch-processor project I am using the mssql package, and I need to gather data from two different SQL databases and then basically emulate a merge operation. My question is, for SQL Server, is there a way I can use IN to pass in an array of IDs as part of the inner SELECT?
To clarify, the IDs I'll be passing in have been gathered from a separate query from a different SQL database. So what I need to do now is pass those IDs into this next query.
I've seen this kind of syntax:
SELECT DISTINCT Name
FROM Production.Product
WHERE ProductModelID IN
(SELECT ProductModelID
FROM Production.ProductModel AS pm
WHERE p.ProductModelID = pm.ProductModelID
AND Name LIKE 'Long-Sleeve Logo Jersey%');
GO
What would it look like to pass in those IDs as part of the inner SELECT statement? Is this something that can be done? Or is there a different way I should approach this?
And to clarify, I do need to do this within my Node project - so calling a stored procedure and using linked tables from my SQL Server database is not an option.
By the way, I could do something like this:
getMatchingIdRecords = async function() {
for (let sourceRecord of sourceArr) {
const matchingIdRecord = await sqlServerQueryHandler(`SELECT NoteDetailsId FROM SR_Empsheets
WHERE NoteDetailsId = ${sourceRecord.notes_detail_id}`);
if (matchingIdRecord) matchingIdRecords.push(matchingIdRecord);
}
return matchingIdRecords;
};
But this is less than ideal because I'm basically hitting the database for every record that is iterated through.
For SQL Server 2016+ you can pass a JSON Array to the query and parse it on the server. Something like:
SELECT NoteDetailsId FROM SR_Empsheets
WHERE NoteDetailsId in ( select value from openjson(#jsonArray) )
Where #jsonArray is a nvarchar(max) parameter that looks like
N'["1323","2311","1234"]'

How can I use a SQL Server table in an openquery to an Oracle database?

I have a database on SQL Server and would like to use a column in one of my tables in a linked server openquery I'm running to an Oracle database in order to match values between the two and insert the result into columns in my table in SQL Server .
Essentially I want it to be like this:
SELECT col1, col2, col3, col4
FROM OPENQUERY(link, 'SELECT * FROM Oracle_Table
WHERE ID = MSSQL.dbo.table.ID`)
So I'd like to be able to use my internal table column values to query an external database. They are related tables but different systems.
Would it be possible to get a big list of the values in the SQL Server table column and use it as a variable in the Oracle query? I've searched extensively online but haven't been able to find this one.
You can't pass parameters like I wanted to, but I ended up creating a bunch of queries in Powershell using a for loop and variables within the string to create my large query, then put a UNION ALL after each SELECT FROM OPENQUERY()

How to get list of tables used in a SQL Server 2005/2008 function?

I'm trying to get all related tables used in a function.
How to get list of tables used in a function in SQL Server 2005/2008?
Try this
SELECT *
FROM information_schema.routines ISR
WHERE CHARINDEX('dbo.FunctionName', ISR.ROUTINE_DEFINITION) > 0

SQL Server 2005 Query for getting multiple records which have same pattern in QC project database

We access the SQL Server 2005 server from the QC Application.
In order to access the data present in the SQL Server 2005 server we use the Dash Board Module in Quality Center Application.
Sorry I could not post images as I am a fresher to stackoverflow.
When I execute the below query I recieved two rows as output.
SELECT *
FROM CYCL_FOLD
WHERE CF_ITEM_NAME LIKE '%Quality Center%'
When I parameterised the entity and when I am using LIKE cause it is returning only row.
SELECT *
FROM CYCL_FOLD
WHERE CF_ITEM_NAME LIKE #FOLDER_NAME#
Please let me know how to get multiple rows using like clause.
SELECT *
FROM CYCL_FOLD
WHERE CF_ITEM_NAME LIKE '%'+ #FOLDER_NAME +'%'

Pivot sql query

I've SQL Server 2005 table
COMPETITOR (Id, Title, cDate, Price)
I want to format the query so that its view in Gridview will be like this:
Please help me writing the sql query.
SQL 2005 supports PIVOT - the Books Online doc is http://msdn.microsoft.com/en-us/library/ms177410(SQL.90).aspx
The main shortcoming I've found with the PIVOT stuff is that you must specify the column names, although you can use a query beforehand and inject the values into a varchar variable and then execute that.