Use Varchar variable in FROM statement - sql-server-2008

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)

Related

Procedure to change auto_increment value on table

I want write procedure which after execute another procedure change auto increment value in my table. Problem is procedure created ,but not works. When i try to run i have error #1210 - Incorrect arguments to EXECUTE. I find similar problem in link : Set AUTO_INCREMENT value through variable in MySql ,but it's not working for me.
I try change #v_value for v_value (normal variable, not #variable). Also I try use '?' in #v_sql but not working too. I try change 'READS SQL DATA' but no matter. Help what's going wrong.
DELIMITER //
CREATE PROCEDURE check_increment_value()
READS SQL DATA
BEGIN
SET #v_value = (SELECT MAX(id_version)+1 FROM versions);
SET #v_sql = CONCAT('ALTER TABLE `wersje` AUTO_INCREMENT = ',#v_value);
PREPARE st FROM #v_sql;
EXECUTE st USING #v_value;
END
//
Thanks for your help :)
You pass the parameter via string interpolation, not via the prepared statement, so you do not need to specify the using clause:
EXECUTE st;

sql stored proc "Default Parameter Values"

I'm trying to set "Default Parameter Values" in stored proc.
i know the syntax TYPE 1:
CREATE PROCEDURE proc_name #param nvarchar(30) = NULL, #param1 nvarchar(60) = NULL
My TOAD 7.3 using mysql 5.1.35 is giving a syntax of TYPE 2: when I create new Procedure
`CREATE PROCEDURE name (param_name param_type)
for e.g: create procedure test (IN name varchar(50))
but i can't find the right syntax to set 'IN' values
create procedure test (in name varchar(50) = null)
is throwing syntax error.
please tell me the right syntax to set default parameters in type 2.
I've searched a lot in and out of SO for this, no luck :\
You can't explicitly do that, but you could simulate it, in some cases. I just now had a case of that, where the parameter I wanted a default value to, was being checked in an IF statement. Here's how I did it:
In the body of my condAddNewLineTo, I had if statement. Since my function was to conditionally return things, I simply put my default behavior outside of those if-statements. Like this:
IF varToCheck = <nonDefaultValue> THEN
#some lines of code to execute
RETURN <value>;
END IF;
# default behavior here
RETURN <defaultValue>;
Alternately, if not writing stored function that returns stuff, you could do something like:
IF varToCheck = <nonDefaultValue> THEN
# some lines of code to execute
ELSE
# some default code
END IF;
Hope this helps!

Variables not recognized in an Execute SQL task

I have this sql statements running in my ssis package. I have hard coded the email address. How can i pass it as variable and externalize the variable and can pass the value from config file?
The #PackageStartTime is the system start time. I have declared that variable and set the expression in property window with #[System::StartTime] and evaluated that expression. But when i run this package and when it hits this particular task it get stuck at there saying PackageStartTime parameter is not recognized and result property is not set correctly.
Here is my code:
DECLARE #PackageStartTime Varchar(250)
SET #PackageStartTime =?
IF (SELECT COUNT(*) FROM [dbo].[Table1] WHERE RowCDate >= #PackageStartTime) > 0
BEGIN
DECLARE #SUB Varchar(250)
SET #SUB = 'File Failed' + ##SERVERNAME
DECLARE #BODY Varchar(250)
SET #BODY = 'File Failed' + ##SERVERNAME
EXEC msdb.dbo.sp_send_dbmail
#profile_name='default',
#recipients='dev#null.com',
#subject=#SUB,
#body=#BODY,
#query= 'SELECT DISTINCT FileLoadName FROM [dbo].[Table1] WHERE RowCDate >= #PackageStartTime',
#attach_query_result_as_file=1
Any idea what to solve these two things?
Thanks in advance.
Maina, I just responded to your another post on this subject. You have three posts so far that I have encountered. The questions you have asked in this one are very general in nature and the answer can be found in any good book (such as Professional Microsoft SQL Server 2008 Integration Services). You can also find complete step-by-step answer on many websites (but do not use them as a replacement of a book; otherwise chances are your knowledge of SSIS will remain patchy.
With that said, here are two links that answer your questions:
1. Passing a variable: https://www.simple-talk.com/sql/ssis/passing-variables-to-and-from-an-ssis-task/ (Actually, it appears I had sent this link to you earlier as well.)
Assigning variable values from config - http://www.bidn.com/blogs/DevinKnight/ssis/1655/passing-values-into-an-ssis-package-at-runtime-from-outside-the-package
Focus on item 1 first. After you have practiced it, create a new Execute sql task and put just his much code inside it:
DECLARE #PackageStartTime Varchar(40)=?;
SELECT COUNT(*) FROM [dbo].[Table1] WHERE RowCDate >= #PackageStartTime;
and store this row count in a package variable iCount. Returning the value is done through ResultSet property. If you are able to make this task work, you have enhanced your proficiency in Execute SQL Task.

Syntax translate

this is my code :
CREATE TRIGGER Zmiana_kategorii
ON Hotele
AFTER UPDATE
AS
BEGIN
DECLARE #stara smallint, #nowa smallint
IF COL_LENGTH('deleted', 'IloscGwiazdek')
BEGIN
SET #stara=(SELECT IloscGwiazdek FROM deleted)
SET #nowa=(SELECT IloscGwiazdek FROM inserted)
IF(#stara<#nowa)
BEGIN
print 'Powiadom następujących klientów o zmianie klasy hotelu'
declare #data date
SET #data=(CONVERT (date, GETDATE()))
SELECT KlientID FROM Rezerwacje Where #data<DataPrzyjazdu
END
END
END
could someone tell me what is wrong in syntax? I am 1st time using MYSQL i have no clue whats is wrong with this...
this is error : ON Hotele AFTER UPDATE AS BEGIN DECLARE #stara smallint, #nowa smallint ' at line 2" this is error
At least one problem is that you don't include the THEN after your IF conditions. For example:
IF (#stara<#nowa) THEN
BEGIN
-- ...
END
Another problem is that you have the order of the CREATE TRIGGER elements wrong. It should be:
CREATE TRIGGER Zmiana_kategorii
AFTER UPDATE
ON Hotele
Note that the AFTER UPDATE goes before the ON. This seems to solve your specific problem.
Finally, MySQL doesn't have a PRINT command and all variables have to be declared at the beginning of a BEGIN block, before any other statements.
Please read this entire page and understand what's going on: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
If you understand the syntax, then you can debug it yourself. These types of simple problems shouldn't require community assistance (though sometimes an extra pair of eyes can catch an obvious error).

could any one help me finding the Error with SP

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.