I am trying to get table name from parameters, then i will use that to my query like
declare #cmd nvarchar(100)
declare #tableName nvarchar(100)
select #cmd = ' select * from ' + #tableName
execute (#cmd)
when i try to run with query designer dataset, it popup error message like
the declare sql construct or statement is not supported
What am I missing that needs to be there or what is causing this issue?
i hope somebody can give a hand how to solve it
thanks
It's just the query designer that cannot support this for obvious reasons. Just click "Edit as text" in the top left corner of the query designer and then you can do whatever you like.
Just remember that your dataset must always return the same column names in the same order with the same datatypes.
Related
Let's say I have the following scenario
Select
age
,address
,full_name
from
[dbo].customers
I want to replace the [dbo] part with an ssis variable, so when we go to run this package and need to replace the schema, it's a piece of cake.
What I have tried so far was creating a SQL variable, named schema_var_name, and appending it to the front, but this is not working out for me.
Can you tell me what I am doing wrong?
Here is what I have:
"Select
age
,address
,full_name
from
'#[User::schema_var_name]' + '.[customers]' "
You will need to concatenate the #[User::schema_var_name] variable within the SQL text, with the other parts of the query enclosed with double-quotes ("). An example of an expression for your SQL command is below. This can then be used as the expression for a variable that is set for the source statement in an Execute SQL Task or source component within a Data Flow Task.
"Select age, address, full_name from " + #[User::schema_var_name] + ".[customers]"
You could also build a dynamic sql statement and execute it in an Execute SQL Task. The ? tells the task that you want to use a variable in that location. You then set up the variable under Parameter Mapping. Your query would then look like this:
DECLARE #schemaName NVARCHAR(100) = ?
DECLARE #sqlQuery NVARCHAR(MAX) = N'
SELECT
age
,address
,full_name
FROM
[' + #schemaName + '].customers'
EXEC(#sqlQuery)
EDIT:
Here's what the Parameter Mapping would look like:
The Parameter Name is most often the least intuitive of these fields. It's a 1-based id sequence for Input parameters. If you had another input variable to use, its Parameter Name would be 2.
I'm wondering if there is a way, after pragmatically giving a Varchar variable a value, to use that variable in the From Statement? We have archive tables that will cycle out and back in. So, the table can go missing for several days and when they cycle back in they change the table name to end w/ the current year (Ex. 012015 when before it was 012014.) Because of this my query can throw up object errors and I'm the only guy on my team that understands the simple error and how to quickly fix the stupid thing. below is the solution I'm trying to get to work, but I keep getting the error "Must declare the table variable #Jan." I totally understand what a table variable is and how to use it and that is obviously not what I want to do here. Is there any way to use a varchar variable (or other similar variable type) in the From Statement? Code below:
Declare #Jan Varchar(40)
IF OBJECT_ID('ColTelephonyArchive.dbo.ACDSkill201401') IS NOT NULL
Begin
Set #Jan = 'ColTelephonyArchive.dbo.ACDSkill201401'
END
IF OBJECT_ID('ColTelephonyArchive.dbo.ACDSkill201501') IS NOT NULL
Begin
Set #Jan = 'ColTelephonyArchive.dbo.ACDSkill201501'
END
IF #Jan IS NULL
Begin
Set #Jan = 'Does.Not.Exist'
END
Select WorkDte, SwitchNbr, SkillNbr,StaffTimeInSec, AvailableTimeInSec, ACDCallTotCt
,AbandonCallTotCt, AbandonCall1Ct, AnswerTimeInSec, ACDTalkTimeInSec,TotAcwTimeInSec, HoldTimeInSec
FROM #Jan
If I'm getting your problem right, you need to build the dynamic query. So in your case it would be
DECLARE #qry VARCHAR(511)
SET #qry = 'Select WorkDte, SwitchNbr, SkillNbr,StaffTimeInSec, AvailableTimeInSec, ACDCallTotCt
,AbandonCallTotCt, AbandonCall1Ct, AnswerTimeInSec, ACDTalkTimeInSec,TotAcwTimeInSec, HoldTimeInSec
FROM ' + #Jan
EXEC (#qry)
I have written a T-SQL script that migrates some data from one database to another.
At the moment I am doing that by use of dynamic sql.
For example see the following code:
Declare #sqlquery nvarchar(4000)
SET #sqlquery = N'SELECT * from ' + #LinkServerName + #SourceDatabaseName + '.dbo.Table'
EXEC #sqlquery
In this example #LinkServerName is a nvarchar variable that stores the name of the linked server for the SQL Server that contains the source database. #SourceDatabaseName is a nvarchar variable that stores the name of the source database.
I donĀ“t like that way. I would prefer the following code:
SELECT * from #SourceDatabase.dbo.Table
Is that possible?
Thank you in advance.
Second approach is incorrect, first one is the correct one. For more information check this other question here at stackoverflow how-to-use-variable-for-database-name-in-t-sql
Is there a way where in I can select multiple dates and pass it as my parameters for a stored proc for a report in ssrs. selecting allow multiple values for a parameter gives a dropdown list. but can i get a calender control where I can select Multiple dates.
SQL Server Reporting Services, as of version 2008R2, does not have this functionality built in. I haven't looked at 2012, but I'd be surprised if it offered this.
(You can always build your own interface using a ReportViewer control, URL access or another access method to display reports.)
As Jamie stated, you can't really do this. The "best" work around I have come across in my experience is to pass your parameter value(s) as one text string, and use a split function to parse in your WHERE condition in the stored proc.
USE [YOUR DATABASE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[split](
#delimited NVARCHAR(MAX),
#delimiter NVARCHAR(100)
) RETURNS #t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
DECLARE #xml XML
SET #xml = N'<t>' + REPLACE(#delimited,#delimiter,'</t><t>') + '</t>'
INSERT INTO #t(val)
SELECT r.value('.','varchar(MAX)') as item
FROM #xml.nodes('/t') as records(r)
RETURN
END
Your parameter would be something like this in your stored proc:
#Parameter VARCHAR(200)
Then your where condition in your stored proc will be something like this
where convert(varchar(10), cast([YOURDATE] as date), 101) IN (select val from dbo.split(#Paramater,','))
I hope this helps!
create function Fun12(#InsCd varchar)
returns varchar(100)
as
begin
declare #ret varchar(52)
set #ret = (select [InsName] from [Assignment4].[dbo].[1466]
where rtrim(ltrim([InsCd]))= #InsCd)
return #ret
end
Executing:
declare #r varchar(50)
exec #r = dbo.Fun12 '436'
select #r
I am getting NULL value.
Could any one please help me finding the error?
You need to specify a size for your parameter #InsCd.
Some thing like this but you might want to use another value than 20 depending on the size of field InsCd.
create function Fun12(#InsCd varchar(20))
First, you should make sure that the code contained by the function actually returns something when you run it directly in SQL Server Management Studio (SSMS):
-- SQL
select InsName from [1466] where rtrim(ltrim([InsCd])) = '436';
In this case, I would use a stored procedure rather than a function. You could also use the SSMS Profiler (Tools > Profiler) to monitor the traffic to SQL Server. This way, you can actually see what gets executed, see the parameters for SPs, etc.