Pass dummy Parameter to SQL Query - sql-server

I am new to sql query and got stuck in middle of some query.
Issue:- my query where condition is like this
"where new_FleetSeries in (#fleet)" now in report everything is working fine.
But i want to make this query run in SQL for testing and in order to test i have created on variable and manually filled the values something like below.
Declare #fleet varchar(150) SET #fleet= ('B777,B777F-200, B777F-200L')
Now if i select the same parameters from report my report gives me result but same parameters if i fill manually didnt gives me result in SQL.

The IN clause must have values not only one string
something like this
where new_FleetSeries in ('B777','B777F-200', 'B777F-200L')
so instead of filling a parameter , you have to concatenate those values to the original query

Related

Hard code SSRS multi value parameter for testing

I'm trying to test a SQL query in SQL Server Management Studio that normally requires a multivalue parameter from the SSRS report its a part of.
I'm not sure to how hard code a multi value parameter in management studio. The report was created by a vendor, I'm just trying to make it runnable for testing outside of SSRS.
For example the parameter in SSRS is a collection of numbers that the user selects - ie "3100, 3102, 3105" would be the selections for the multivalue parameter called #object_code
I've got something like this - but it's not working.
Declare #Object_Code varchar(100)
Set #object_Code = ('3100','3102','3105')
....really long vendor written query I don't thoroughly understand...
IN(#object_code)
You have to use String-Split function to separate comma separated values.
For example-
Declare #Object_Code varchar(100)
Set #Object_Code = '3100,3102,3105'
....really long vendor written query I dont thoroughly understand...
--T.object_code IN (#object_code)
Inner Join dbo.Split(#Object_Code, ',') as S On S.data = T.object_code
Search your database first for any string-split function.
If you want to create string-split function then follow this -
T-SQL split string
If you use SQL Server 2016 you might want to check out the function STRING_SPLIT.
If you use a lower version of SQL Server and you can't or don't want to create a separate function, the following could be an alternative:
declare #object_code varchar(100);
set #object_code = '3100,3102,3105';
select
ltrim(rtrim(x.par.value('.[1]','varchar(max)'))) as object_code
from (
select convert(xml,'<params><param>' + replace(#object_code,',', '</param><param>') + '</param></params>') as c
) tbl
cross apply
c.nodes('/params/param') x(par);
Everybody seems to be getting hung up on splitting a string that doesn't have to be a string. We're just trouble shooting a query here and need a way to feed it values. It's not important how SSRS does it, just that we can reproduce the result.
Declare #Object_Code table (params varchar(20));
INSERT #object_Code
VALUES ('3100'),('3102'),('3105')
....really long vendor written query I don't thoroughly understand...
IN (SELECT params FROM #object_code)
Then spend some quality time getting to know the query.

How can I select a date from a table and pass it in as a parameter in SSIS?

I have one single date in one table. I am trying to select this date and pass it into a function that pulls data from another system, based on this date parameter. I think it should be something like this.
Declare #dateCurrent as DateTime
Set #dateCurrent = MyDate
SELECT #dateCurrent = DATEADD(Day,-1,CONVERT(VARCHAR(6),#dateCurrent,112)+'01')
Select top 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', #dateCurrent)
I set 'MyDate' as a Variable. I'm not sure if this is the right way to do it. Or, should I simply select the date from the table, like this.
Select AsOfDate
FROM DATE_RAW_DATA
No matter what I try, I'm getting an error that SSIS can't execute the query because the date parameter isn't coming in. How can I get this setup correctly? Thanks.
First, create an Execute SQL Task that gets the date value from your table and saves it into your variable.
Next, create another Execute SQL Task that is connected after the first task, and then use your variable in the query. Assuming you have the date saved in a variable named MyDate, your query for the second SQL Task would look like this:
SELECT TOP 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', DATEADD(DAY,-1,CONVERT(VARCHAR(6),?,112)+'01'))
The question mark in the query means you're going to be using an input parameter for that value. So once you have the SQLStatement field value set, click on the "Parameter Mapping" option in the left pane of the Execute SQL Task Editor. For "Variable Name", select your variable from the drop down list, and for "Parameter Name" put 0 (which corresponds to the first question mark in the query). Make sure the variable data type and size are set correctly.

SSRS parameterized query slow on Sharepoint

Having an issue when running a report with a cascading parameter on Sharepoint.
After you enter the first parameter (#Suburb) it takes approx. 10sec before displaying the drop-down list of postcodes. Running SQL Server Profiler I can see that the actual query returns in 9ms.
The dataset is just a query, not a stored procedure, for the second parameter and looks like this:
SELECT PostCode
FROM SuburbData
WHERE Locality = #Suburb
So far I have tried the following answers found on here, such as assigning the parameter to a variable like so:
DECLARE #Locale CHAR(100)
SET #Locale = #Suburb
SELECT PostCode
FROM SuburbData
WHERE Locality = #Locale;
Adding the OPTION (RECOMPILE) hint to the end of the query.
Creating a stored procedure of the above query.
The parameter is of data type TEXT.
Why the delay in displaying the result?

Dynamic SQL Insert: Column name or number of supplied values does not match table definition

I encounter some strange behavior with a dynamic SQL Query.
In a stored procedure I construct an insert query string out of multiple Strings. I execute the insert query in the SP like that - due to single nvarchar length restrictions.
EXEC(#QuerySelectPT+#QueryFromPT+#QueryFromPT)
If I print each part of the query, put these parts together and execute them manually in Management Studio the query works fine and inserts the data. But, if i execute the query in the EXEC() Method in the stored procedure, I get a
Column name or number of supplied values does not match table definition.
Error Message.
Did multiple check on the amount, spelling of columns in my query and in my insert table, but I have not found any differences so far.
Any advices?
Your count of columns for insert are different from count of columns for select. Print the statement before exec and find the error.
It as shot in the dark but seen you are telling the queries are valid and if you build the final query manually and it is working, the issue could be caused by string truncation.
Could you try:
EXEC(CAST(#QuerySelectPT AS VARCHAR(MAX))+#QueryFromPT+#QueryFromPT);
Also, as the Management Studio's message tab and selects are limited to 4000 symbols I think, you can test if the whole query is assembled correctly like this:
SELECT CAST(#QuerySelectPT+#QueryFromPT+#QueryFromPT AS XML)

Null value being returned from a Stored Procedure using Linq to SQL

If I run a stored procedure in my database (SQL 2000) and return the SCOPE_IDENTITY, I receive a value. However, using LINQ to SQL, I get a null value for the Column1 property. Anyone have a suggestion?
Make sure you have only one SELECT statement at the end of your SP. So, something like this:
IF something
DO something
SET variables
ELSE
SET variables
SELECT variables
Also, rename your column in the select statement and possibly cast it into proper data type. Something like this:
SELECT CAST(#variable AS DataType) AS [ColumnName];
I ran into this a week ago because I had 2 SELECT statements in a SP and they were screwing each other up. The SP always did what it was supposed to but it always sent null back. Since I switched to what I wrote above it's worked like a charm.
Hope this helps you

Resources