Mapping a user defined function with SSRS package parameters - sql-server

I have created the following User Defined Function :
CREATE FUNCTION MyTable
(
#dd_YearNo INT,
#txt_WeekNoFrom INT,
#txt_WeekNoTo INT,
#dd_Group VARCHAR(MAX),
#dd_Team VARCHAR(MAX),
#dd_SalesPerson NVARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN
SELECT *
FROM T1 (NOLOCK)
WHERE YearNo IN (#dd_YearNo)
AND (WeekNo BETWEEN #txt_WeekNoFrom AND #txt_WeekNoTo )
AND LEFT(Team,2) IN (#dd_Group)
AND Team IN (#dd_Team)
AND SalesPerson IN(#dd_SalesPerson)
I am using it as a my data set n SSRS like below :
How to map the parameters I created for my SSRS package to this user defined query. This is waht I get since I validate it :
These are the parameters I am using in my SSRS package and I am getting them from different queries :
I get the following error :
Invalid Object MyTable

Related

Error parsing JSON exception for xml filed in copy command Snowflake

Hi I have declared a table like this
create or replace table app_event (
ID varchar(36) not null primary key,
VERSION number,
ACT_TYPE varchar(255),
EVE_TYPE varchar(255),
CLI_ID varchar(36),
DETAILS variant,
OBJ_TYPE varchar(255),
DATE_TIME timestamp,
AAPP_EVENT_TO_UTC_DT timestamp,
GRO_ID varchar(36),
OBJECT_NAME varchar(255),
OBJ_ID varchar(255),
USER_NAME varchar(255),
USER_ID varchar(255),
EVENT_ID varchar(255),
FINDINGS varchar(255),
SUMMARY variant
);
DETAILS column will contain xml file so that i can run xml function and get element of that xml file .
My sample rows looks like this
dfjkghdfkjghdf8gd7f7997,0,TEST_CASE,CHECK,74356476476DFD,<?xml version="1.0" encoding="UTF-8"?><testPayload><testId>3495864795uiyiu</testId><testCode>COMPLETED</testCode><testState>ONGOING</testState><noOfNewTest>1</noOfNewTest><noOfReviewRequiredTest>0</noOfReviewRequiredTest><noOfExcludedTest>0</noOfExcludedTest><noOfAutoResolvedTest>1</noOfAutoResolvedTest><testerTypes>WATCHLIST</testerTypes></testPayload>,CASE,41:31.3,NULL,948794853948dgjd,(null),dfjkghdfkjghdf8gd7f7997,test user,dfjkghdfkjghdf8gd7f7997,NULL,(null),(null)
When i declare DETAILS as varchar i am able to load file but when i declare this as variant i get below error for that column only
Error parsing JSON:
dfjkghdfkjghdf8gd7f7997COMPLETED</status
File 'SNOWFLAKE/Sudarshan.csv', line 1, character 89 Row 1, column
"AUDIT_EVENT"["DETAILS":6]
Can you please help on this ?
I can not use varchar as i need to query element of xml also in my query .
This is how i load into table and i use default CSV format ,file is available in S3 .
COPY INTO demo_db.public.app_event
FROM #my_s3_stage/
FILES = ('app_Even.csv')
file_format=(type='CSV');
Based on Answer this is how i am loading
copy into demo_db.public.app_event from (
select
$1,$2,$3,$4,$5,
parse_xml($6),$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,parse_xml($17)
from #~/Audit_Even.csv d
)
file_format = (
type = CSV
)
But when i execute it says zero row processed and no mentioned stage here
If you are using a COPY INTO statement then you need to put in a subquery to convert the data before loading it into the table. Use the parse_xml within your copy statement's subquery, something like this:
copy into app_event from (
select
$1,
parse_xml($2) -- <---- "$2" is the column number in the CSV that contains the xml
from #~/test.csv.gz d -- <---- This is my own internal user stage. You'll need to change this to your external stage or whatever
)
file_format = (
type = CSV
)
It is hard to provide you with a good SQL statement without a full example of your existing code (your copy / insert statement). In my example above, I'm copying a file in my own user stage (#~/test.csv.gz) with the default CSV file format options. You are likely using an external stage but it should be easy to adapt this to your own example.

Inline User Defined Function for a view having parameters

I am using an openquery to get my data. Because of the length of my query which exceeds 8000 characters, I am using this approach :
DECLARE #myStatement VARCHAR(MAX)
SET #myStatement 'SELECT * FROM Employee...'
EXECUTE (#myStatement) AT [MyLinkedServer]
Knowing that it is not possible to use variables in a view, I am trying to use a User Defined function that returns a table like below :
CREATE FUNCTION dbo.EmployeeDetailsForHR
(#myStatement VARCHAR(MAX))
RETURNS TABLE
AS RETURN(EXECUTE (#myStatement) AT [MyLinkedServer]);
SELECT *
FROM dbo.EmployeeDetailsForHR('SELECT * FROM Employee...');
But my CREATE FUNCTION isn't correct. What is the correct syntax in my case?

what is the error? (SELECT [Data] FROM Split(#ProductName,','))

The stored procedure that used work with multiple parameters but it stops working.
I am trying to fix the stored procedure that used to work but suddenly create an error 'Invalid object name 'Split''. This is the procedure that someone else wrote so I am not exactly sure what 'Split' is. Is this some sort of command or what?
This is the stored Procedure that used to work but not anymore.
DECLARE #ProductName NVARCHAR(MAX) = '9x95dk36-2727-9401-8948-161740000000,150t3vh6-1230-4449-8846-173120000000'
SELECT
m.[member_id]
, m.external_member_id
, m.last_name
, m.first_name
, m.middle_name
, e.effective_date
,[termination_date]
, REPLACE(bhp.name_full_path,'CW > Medicaid > WI > SSI > ','') AS BHP
, pr.product_name
,pr.product_ID
INTO #ActiveSSIMembers
FROM
[Eligibility] as e
INNER JOIN Product as pr on e.product_id = pr.product_id
INNER JOIN Member as m on e.member_id = m.member_id
INNER JOIN BhpNode as bhp on m.bhp_node_id = bhp.bhp_node_id
WHERE
pr.product_ID IN (SELECT [Data] FROM Split(#ProductName,','))
We need to use multiple parameters for the query.
From the code you have posted, it seems Split is a Table-Valued User-Defined function that takes NVarchar parameter(seperated by ',') and returns rows of split values under the column name Data.
You are missing that user-defined function in your database where this query is being executed. Either this has been removed or you running the query in a different database.
Try navigating to Table-Valued Functions as below and make sure you have a function called Split in there(instead of 'TestFunction' in that image). If not, you can create your our function to split the values and add it to your database.
You can take help from this thread to create one

Querying XML Data Using SQL Parameters

I am trying to query XML data in a table using a SQL parameter containing the search criteria and I am getting unexpected results.
As an example, consider the following T-SQL code:
DECLARE #Configuration AS TABLE
(
[Data] XML NOT NULL
);
INSERT INTO Configuration ([Data]) VALUES '<Configuration><Colors><Color>RED</Color><Color>BLUE</Color></Colors></Configuration>');
DECLARE #Color AS NVARCHAR(MAX);
SET #Color = N'YELLOW';
SELECT COUNT(*) FROM #Configuration WHERE [Data].exist('/Configuration/Colors/Color/text() = sql:variable("#Color")') = 1;
I would expect the result of this query to return 0. In fact, this query returns 1. Even worse, it appears to return one regardless of what value I set #Color to.
What am I missing?
You have missing [ in your query
SELECT COUNT(*) FROM #Configuration WHERE [Data].exist('/Configuration/Colors/Color[text() = sql:variable("#Color")]') = 1;
XPath expressions are supposed to be enclosed between [ and ]. For more information on XPath and writing expressions, please refer this tutorial page.

list records from sql server UDF that returns Table in classic asp

I wrote my first sql Server returning table UDF since thought was better than using a SP
but, while can easily retrieve the result from sql server.. I can't get result calling it from classic ASP ADO
UDF is as follows:
CREATE FUNCTION dbo.udf_Alert
(
#I nvarchar (30),
#L nvarchar (10)
)
RETURNS TABLE AS RETURN
(
SELECT a.Message, a.Height, a.backgroundColor, a.isFree
from Advice a
join ActiveMessages b on b.MessageID=a.MessageID
join Items i on b.ItemID=i.ItemID
join Sellers s on i.UserID=s.UserID
join Users u on u.UID=s.UID
WHERE
(i.ItemID=#I and a.Active='1' and b.Active='1' and i.active='1' and i.Show='1' and CHARINDEX('ALERT',u.Modules)>0
and a.ValidFrom<GETDATE() and a.ValidTo>GETDATE() and u.PaidUntil>GETDATE() and charindex(#L,a.Languages)>-1 or charindex('all',a.Languages)>-1 )
UNION ALL
SELECT a.Message, a.Height, a.backgroundColor, a.isFree
FROM Advice a, Users u
WHERE u.isFree='1' and a.isFree='1' and (CHARINDEX(#L,a.Languages)>-1 or Charindex('all',a.Languages)>-1)
)
and I can easily execute from SSMS calling
Select * from dbo.udf_Alert('281F50246','fr')
But I have to embed into a classic ASP routine but I've not found the way to do it..
tried the SP method.. but I got error when try to set the parameters:
here what I tried:
sql="Select dbo.udf_Alert('xx','yy')"
dim cmdA
set cmdA = Server.CreateObject("ADODB.Command")
cmdA.ActiveConnection= cn
cmdA.CommandType=4
cmdA.CommandText=sql
cmda.Parameters.Append cmdA.CreateParameter("fd", nvarchar, adParamInput,30, itemID)
cmda.Parameters.Append cmdA.CreateParameter("fde", nvarchar, adParamInput,10, LanguageID)
' cmdA.Parameters("#I")=ItemID '<-----ERRROR HERE
' cmdA.Parameters("#L")=LanguageID
set rs=cmdA.Execute()
so I tried set Parametrs in other way.. but got same result:
ADODB.Command error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Can suggest some advice?
Thanks
Sergio
We tend to create stored procedure "wrappers" for UDFs on a one-to-one basis which can be called directly from web code. For your example, this might look like
CREATE PROCEDURE [dbo].[pr_Alert]
#I nvarchar (30),
#L nvarchar (10)
AS
SELECT * FROM testDb.dbo.udf_Alert(#I, #L)
RETURN
Your commandText would then be simply the name of the wrapper SP, "pr_Alert".
Hope this helps.

Resources