Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Tuesday, March 27, 2012

Creation of "global" variables in runtime.

Is it possible to create "global" (package scope) variables in runtime in SSIS packages? If yes, please give some examples.

Hi Yuriy:

Not sure I understand your question. Are you asking how to create variables that are consumed at runtime, or to have on-the-fly variables created as a package runs?

The first type is easy -- go to the Control Flow tab, make sure nothing is selected, and right-click anywhere in the white space outside of an object, and select "Variables" from the shortcut menu. Alternatively, select "Variables" from the SSIS menu at the top of the main window.

The dialog you see is pretty self-explanatory. Click on the blue "x" button in the toolbar to add a new variable. Give it a name, set its data type and initial value. Delete a variable by selecting it in the list and clicking the X button with the red "x" over it.

Once you create a variable it can be referenced through code in a Script Task or expressions in other tasks. Most often you must reference the variable as @.[User::<VariableNameHere>]. Notice the two colons (::) between "User" and the variable name, and the @. sign in front of the expression.

If an object in the Control Flow tab is selected when you invoke the Variables command, the variable is local to that object. I do this now and then on containers when I need a variable that is used to share data among the tasks within a container.

I don't believe it's possible to create entirely new variables at runtime.

|||No object can be created at runtime inside the packge.

Sunday, March 25, 2012

creating variables in a Stored procedure

I would like to know if I can create new variables using existing variables in a stored procedure.

To be clear, in the SP I use, I pass table name. But I need the results from 2 other tables as well.

The tables are named 1996, 1997, 1998.......2007.
If I pass 2005 to SP, I need results from 2005, 2004 & 2003. How do I assign or get 2 new table names (variables) for 2004 & 2003 ?

Part of code:
ALTER Procedure [dbo].[XX](
@.TblName1 varchar(20),
@.Month varchar(3)
)

when I pass 2005 to SP, I need
@.TblName1 = 2005
@.TblName2 = 2004
@.TblName3 = 2003

How can assign 2004 & 2003 to variables TblName2 & Tblname3 ?

I really appreciate any help.

look up either the SELECT statement or the SET statement in books online. But you seem to have an inconsistency in the way you are using the data. Your code has:

Code Snippet

ALTER Procedure [dbo].[XX](
@.TblName1 varchar(20),
@.Month varchar(3)
)

when I pass 2005 to SP, I need
@.TblName1 = 2005
@.TblName2 = 2004
@.TblName3 = 2003

But you seem to be subtracting 1 and 2 from parameter passed to the proedure. But if that is what you want, you need something like:

Code Snippet

ALTER Procedure [dbo].[XX](
@.TblName1 varchar(20),
@.Month varchar(3)
)

as

declare @.TblName2 varchar(20)

declare @.TblName3 varchar(20)

set @.TblName2 = convert(integer, @.TblName1) - 1

set @.TblName3 = convert(integer, @.TblName1) - 2

But a big problem now is that if you pass a string in for you table name such as 'aTable'

you will get execution errors.

You can avoid this by use of IF statements such as:

Code Snippet

IF isNumeric(@.TblName1) = 1

select @.TblName2 = convert(integer, @.TblName1) - 1,

@.TblName3 = convert(integer, @.TblName2)

Now if that isn't enough, there are times (actually, many times) in which the "IsNumeric" function will not correctly assess whether or not a string is numeric. There is an article that talks about these particular problems here:

http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html

Look for the "IsReallyNumeric" funtion at this site. That should be enough to keep you going for at least a little while.

Kent



|||

Miamik,

This has all the markings of a very unwieldy design. I would not consider having identical tables named as this seems to indicate JUST as a method to partition data.

You would be better served to explore table partitioning in Books Online. Then your code would be so-o-o-o much easier, and you wouldn't have to resort to using dynamic SQL (with all of its' issues and security problems.)

|||I think Arnie is definitely telling it like it is.|||Thanks for all your replies.

Thursday, March 22, 2012

Creating tables with variables value?

Hi I would like to know how can I create a table with a veriable from my code.

This is my code:

DECLARE @.MTH INT
DECLARE @.MTH2 INT
DECLARE @.DAY INT
DECLARE @.BGN DATETIME
DECLARE @.END DATETIME
DECLARE @.ENDI INT
DECLARE @.DTCTRL DATETIME
DECLARE @.TBLNAME TABLE (NOME VARCHAR(30))
--||_@.MTH_ ===>_RANGE_MONTH/DAYS||
SET @.MTH = 11 + 1
_RANGE_MONTH">--||_@.MTH2_===>_RANGE_MONTH__||
SET @.MTH2 = 5
--||_||

SET @.BGN = CONVERT(VARCHAR(8),DATEADD(MM,-@.MTH,GETDATE()),112)
SET @.END = CONVERT(VARCHAR(8),GETDATE(),112)
SET @.DAY = 0

WHILE @.BGN <> @.END
BEGIN
WHILE CONVERT(VARCHAR(8),DATEADD(DD,-@.DAY,GETDATE()),112) = @.END
BEGIN
INSERT INTO @.TBLNAME
SELECT 'TB_CHEQUE_' + CONVERT(VARCHAR,DATEADD(DD,-@.DAY,GETDATE()),112)
SET @.DAY = @.DAY + 1
END
SET @.DTCTRL = CONVERT(VARCHAR,DATEADD(DD,-@.DAY,GETDATE()),112)
SET @.END = DATEADD(DD,-1,@.END)
END

SET @.ENDI = 0
WHILE @.MTH2 <> @.ENDI
BEGIN
INSERT INTO @.TBLNAME
SELECT 'TB_CHEQUE_' + LEFT(CONVERT(VARCHAR,DATEADD(MM,-@.ENDI,@.DTCTRL),112),6)
SET @.ENDI = @.ENDI + 1
END
SET @.DTCTRL = DATEADD(MM,-@.ENDI,@.DTCTRL)
SET @.ENDI = 0


WHILE @.ENDI <> 6
BEGIN
INSERT INTO @.TBLNAME
SELECT 'TB_CHEQUE_' + CAST(DATEPART(YY,DATEADD(YY,-@.ENDI, GETDATE())) AS VARCHAR)
SET @.ENDI = @.ENDI + 1
END

SELECT * FROM @.TBLNAME

The name of the table is in each line of the table @.TBLNAME !

Any help ?

Can you please rephrase your question and perhaps give a repro? I'm not sure what it is that you are asking.

Thanks|||For each row of the table @.TBLNAME I need to create a table with that Name and i don't know how to do that|||

Like this -
usage:
exec InsertIntoTableName 'temptablename'

create proc InsertIntoTableName(@.TBLNAME sysname)
as
DECLARE @.MTH INT
DECLARE @.MTH2 INT
DECLARE @.DAY INT
DECLARE @.BGN DATETIME
DECLARE @.END DATETIME
DECLARE @.ENDI INT
DECLARE @.DTCTRL DATETIME
DECLARE @.sql VARCHAR(8000)
SET @.SQL = 'create table ##'+@.TBLNAME +'(NOME VARCHAR(112))'
print @.SQL
exec (@.SQL)
--||_@.MTH_ ===>_RANGE_MONTH/DAYS||
SET @.MTH = 11 + 1
_RANGE_MONTH">--||_@.MTH2_===>_RANGE_MONTH__||
SET @.MTH2 = 5
--||_||

SET @.BGN = CONVERT(VARCHAR(8),DATEADD(MM,-@.MTH,GETDATE()),112)
SET @.END = CONVERT(VARCHAR(8),GETDATE(),112)
SET @.DAY = 0

WHILE @.BGN <> @.END
BEGIN
WHILE CONVERT(VARCHAR(8),DATEADD(DD,-@.DAY,GETDATE()),112) = @.END
BEGIN
SET @.SQL = 'INSERT INTO ##'+ @.TBLNAME + ' SELECT ''TB_CHEQUE_' + CONVERT(VARCHAR,DATEADD(DD,-@.DAY,GETDATE()),112) +''''
EXEC (@.SQL)
SET @.DAY = @.DAY + 1
END
SET @.DTCTRL = CONVERT(VARCHAR,DATEADD(DD,-@.DAY,GETDATE()),112)
SET @.END = DATEADD(DD,-1,@.END)
END

SET @.ENDI = 0
WHILE @.MTH2 <> @.ENDI
BEGIN
SET @.SQL = ' INSERT INTO ##'+ @.TBLNAME + ' SELECT ''TB_CHEQUE_' + LEFT(CONVERT(VARCHAR,DATEADD(MM,-@.ENDI,@.DTCTRL),112),6) +''''
EXEC (@.sql)
SET @.ENDI = @.ENDI + 1
END
SET @.DTCTRL = DATEADD(MM,-@.ENDI,@.DTCTRL)
SET @.ENDI = 0


WHILE @.ENDI <> 6
BEGIN
SET @.SQL='INSERT INTO ##'+@.TBLNAME + ' SELECT ''TB_CHEQUE_' + CAST(DATEPART(YY,DATEADD(YY,-@.ENDI, GETDATE())) AS VARCHAR)+''''
EXEC (@.sql)
SET @.ENDI = @.ENDI + 1
END

exec ('SELECT * FROM ##'+@.TBLNAME)

sql

Tuesday, March 20, 2012

Creating Stored Procedures Based On Variables

Hi There,

I would like to know if for example i have some tables in my DataBase and i need from each table to get for instance, the Name by the ID, how can i make only one procedure which use parameters and variables so i can use this SP to get the Name By ID for all the Tables?

Thanks

You would have to use dynamic SQL in the stored procedure. Assuming all you tables had a unique column named ID and an associated column named NAME, you could create a simple procedure like:
Create Procedure GetNameByID(

@.tableName varchar(100),

@.ID int

)

AS

Begin

Declare @.sql nvarchar(1000)

Set @.sql = N'Select "NAME" From ' + @.tableName + N' Where ID = ' + Convert( varchar(10), @.ID )

exec( @.sql )

End

go

exec GetNameByID 'sysobjects', 1

exec GetNameByID 'syscolumns', 1

And you could get more complicated from there.

Wednesday, March 7, 2012

creating procedures

I have seen developers create procedures by declaring the variables and
others declaring the variables and setting them to null like below.
Is one way better that another?
CREATE PROCEDURE [dbo].[testsp_InsertIssuerDetails]
@.issuerId int output,
@.companyId int = NULL,
@.issuerName varchar(60) = NULL,
@.dateAdded datetime = NULL,
@.dateModified datetime = NULL,
@.statusDate datetime = NULL,
@.modifiedBy smallint = NULL ....
thanks
MeenalWhen declaring parameters of a procedure the declaration can include a
default value for each parameter. This is what your example shows. This way
the parameters become optional.
On the other hand, the declaration of a variable cannot include a default
value.
ML
http://milambda.blogspot.com/|||If a parameter to a stored procedure has a default assigned, the
parameter is optional when called. If no default is defined, the
parameter is required, and if not provided it will fail:
exec sp_helpindex
Server: Msg 201, Level 16, State 4, Procedure sp_helpindex, Line 0
Procedure 'sp_helpindex' expects parameter '@.objname', which was not
supplied.
Roy Harvey
Beacon Falls, CT
On Tue, 27 Jun 2006 00:48:47 -0400, "meenal"
<meenal_dhody@.hotmail.com> wrote:

>I have seen developers create procedures by declaring the variables and
>others declaring the variables and setting them to null like below.
>Is one way better that another?
>CREATE PROCEDURE [dbo].[testsp_InsertIssuerDetails]
> @.issuerId int output,
> @.companyId int = NULL,
> @.issuerName varchar(60) = NULL,
> @.dateAdded datetime = NULL,
> @.dateModified datetime = NULL,
> @.statusDate datetime = NULL,
> @.modifiedBy smallint = NULL ....
>thanks
>Meenal
>|||IF they are defined in the CREATE statement, what 'looks' like variables are
really known as 'Parameters'. Variables are defined in the stored procedure
body with the DECLARE statement.
It's often easy to confuse Parameters and Variables.
When Parameters are provided values when they are defined, two things are no
w possible. First, it is not necessary to provide a value for that Parameter
when the stored procedure is executed. Second, it may be that a particular
value 'should' be used it the stored procedure if no value of offered.
In Query Analyzer, try the following example:
CREATE PROCEDURE dbo.MyTest
( @.CheckValue int = 0
, @.DateInput datetime = NULL
)
AS
IF @.DateInput IS NULL
SET @.DateInput = getdate()
IF @.CheckValue = 0
PRINT 'Choice Number 0 Selected on ' + cast( @.DateInput AS varchar(11) )
ELSE IF @.CheckValue = 1
PRINT 'Choice Number 1 Selected on ' + cast( @.DateInput AS varchar(11) )
ELSE
PRINT 'Choice Number ' + cast( @.CheckValue AS varchar(5) ) + ' Selected on '
+ cast( @.DateInput AS varchar(11) )
GO
EXECUTE dbo.MyTest
EXECUTE dbo.MyTest @.CheckValue = 1
EXECUTE dbo.MyTest @.CheckValue = 25, @.DateInput = 'jan 1 2005'
--(Note: it is a good practice to use name=value pairs when calling stored p
rocedures and passing parameters.
--It makes your intention very clear, and protects against future changes to
the stored procedure.)
-- Clean up when finished
--DROP PROCEDURE MyTest
--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"meenal" <meenal_dhody@.hotmail.com> wrote in message news:uathBVamGHA.2112@.TK2MSFTNGP04.phx
.gbl...
>I have seen developers create procedures by declaring the variables and
> others declaring the variables and setting them to null like below.
>
> Is one way better that another?
>
> CREATE PROCEDURE [dbo].[testsp_InsertIssuerDetails]
> @.issuerId int output,
> @.companyId int = NULL,
> @.issuerName varchar(60) = NULL,
> @.dateAdded datetime = NULL,
> @.dateModified datetime = NULL,
> @.statusDate datetime = NULL,
> @.modifiedBy smallint = NULL ....
>
> thanks
>
> Meenal
>
>

Sunday, February 19, 2012

Creating Global Variables

I wish I could set a global value on my project so this one value could be
used by each of my reports. Is it possible?Eric,
Global variables defined in your project isn't possible. But you can
consider to use the underlying datasource for this purpose. Just retrieve in
all your report a datasource with the 'global' variables. Be sure that you
put all your variables in one row and name the columns as your variablenames.
Jan Pieter Posthuma
"Eric" wrote:
> I wish I could set a global value on my project so this one value could be
> used by each of my reports. Is it possible?
>

Friday, February 17, 2012

Creating Dynamic Variables

Is there any way I can create variables dynamically in SQL server procedures?

e.g., I initially declare variable
@.col1='1'
@.col2='0'

and in a for loop with variable @.i,

I have @.str='@.col'+@.i
if @.i=1 then the @.str='@.col1'
and try to insert the value this variable holds into a table as

insert into table1 values(@.str)

Here I want to insert the value of @.col1(which is 1) and NOT '@.col1'

I tried so many time unsuccessfully.Has anyone done this kind of stuff before?any work arounds for this?

appreciate u'r help..

Thanks!Chances are you can accomplish your objectives using standard SQL, with maybe a little help from temporary tables, table variables, or case statements. I don't understand what you are trying to do, but your code looks like SQL written by a VB programmer (loops, inserting parameters, etc...). These are legitimate tools for SQL, but most procedures don't require them.

Using a dynamic SQL statement may be an option, either to assign the value to @.str or for the insert statement, but dynamic SQL is tricky because it executes in a distinct environment and your variables go out of scope.

If you could briefly describe your application, relevant table structures, and the task you are trying to perform, I may be able to give your some programming algorythms that are more appropriate for SQL.

blindman