Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Wednesday, March 28, 2012

Persistant variables

Greetings SSIS friends.

Is it possible to create a persistant variable in an SSIS package?

dreameR.78 wrote:

Greetings SSIS friends.

Is it possible to create a persistant variable in an SSIS package?

Meaning what, exactly?|||

The variable retaining its value after the package is run.

|||

dreameR.78 wrote:

The variable retaining its value after the package is run.

I suppose there are progmatic ways of doing this, but the "easy" way that I can see is to use SQL Server based package configurations. At the end of your package, you issue an Execute SQL Task to update the configuration table to set the value of the variable in the table to that variable's current value.|||

Hi Phil,

I will try your suggested method.

Thank you.

Persistant VariableName error in Row Count task

I am getting this error:

The variable "MyVariable" specified by VariableName property is not a valid variable. Need a valid variable name to write to.

This is my scenario:

I had a Row Count task on a data flow that was writing to a locally scoped variable (called MyVariable for this example.)

I needed to access the value of this variable at the Control Flow (global scope), so I deleted it and recreated it at that level.

My new variable has the same name as the old variable, just different scope.

Now I get this error every time I run the package.

BUT WAIT THERE'S MORE!

I have another Row Count step in the same data flow that is presently writing to a globally scoped variable called "ErrorRows."

If I change this step to write to MyVariable it works fine. If I change my other step to use ErrorRows, it works fine. If I change them back I get the error again.

I have tried deleting and recreating the step, and the variable, and using different names for them. Something is very jiggy with this variable!!

Variables names are case sensitives; make sure you are using the proper casing and that the variable is defined in the right scope.

|||

Can you share the package?

|||

I have discovered that the problem lies not in the Row Count task, but somehow in the Script task that immediately follows it.

I still haven't been able to isolate the issue, but for some reason the error is showing as related to the Row Count task.

(That is why I had been unable to shake the error despite deleting and re-creating the task.)

The script is unexceptional. It is simply incrementing a counter to generate a new row id. For some reason it is using the row count, but since the row count in my present tests is 0 the script should not be executing at all, and the error is not an execution error.

In answer to the suggestion above, the yes the case of the variable name is correct. The steps are all passing validation (no little red circles with x's in them) but failing at runtime.

Dylan.

|||Do you have the variable you are referencing in the Row Count marked as read or read/write in the script component? If so, the issue may be that the variable is locked when the Row Count tries to access it.|||

I have removed the variable altogether from the script step, as I realised on reflection that it really wasn't necessary there.

This really was confounding behaviour, however. The way in which the row count step was flagged in error made it tricky to diagnose what was wrong, so I hope that this thread helps out someone else.

I was unable to determine why the script step was having trouble with the variable. I suspect that the engine was trying to run both count and script at the same time, and there was contention for the variable. The variable was read-only in the script step, however.

Anyway, now onto the next issue...

Persistant VariableName error in Row Count task

I am getting this error:

The variable "MyVariable" specified by VariableName property is not a valid variable. Need a valid variable name to write to.

This is my scenario:

I had a Row Count task on a data flow that was writing to a locally scoped variable (called MyVariable for this example.)

I needed to access the value of this variable at the Control Flow (global scope), so I deleted it and recreated it at that level.

My new variable has the same name as the old variable, just different scope.

Now I get this error every time I run the package.

BUT WAIT THERE'S MORE!

I have another Row Count step in the same data flow that is presently writing to a globally scoped variable called "ErrorRows."

If I change this step to write to MyVariable it works fine. If I change my other step to use ErrorRows, it works fine. If I change them back I get the error again.

I have tried deleting and recreating the step, and the variable, and using different names for them. Something is very jiggy with this variable!!

Variables names are case sensitives; make sure you are using the proper casing and that the variable is defined in the right scope.

|||

Can you share the package?

|||

I have discovered that the problem lies not in the Row Count task, but somehow in the Script task that immediately follows it.

I still haven't been able to isolate the issue, but for some reason the error is showing as related to the Row Count task.

(That is why I had been unable to shake the error despite deleting and re-creating the task.)

The script is unexceptional. It is simply incrementing a counter to generate a new row id. For some reason it is using the row count, but since the row count in my present tests is 0 the script should not be executing at all, and the error is not an execution error.

In answer to the suggestion above, the yes the case of the variable name is correct. The steps are all passing validation (no little red circles with x's in them) but failing at runtime.

Dylan.

|||Do you have the variable you are referencing in the Row Count marked as read or read/write in the script component? If so, the issue may be that the variable is locked when the Row Count tries to access it.|||

I have removed the variable altogether from the script step, as I realised on reflection that it really wasn't necessary there.

This really was confounding behaviour, however. The way in which the row count step was flagged in error made it tricky to diagnose what was wrong, so I hope that this thread helps out someone else.

I was unable to determine why the script step was having trouble with the variable. I suspect that the engine was trying to run both count and script at the same time, and there was contention for the variable. The variable was read-only in the script step, however.

Anyway, now onto the next issue...

Wednesday, March 7, 2012

Permission to use table variable?

I am trying to get around business logic that does not give the user permissions to create any temp tables or tables.

If a user falls into this category, will they still be allowed to create "table variables"? (Declare @.x table (i int)

...I'm obviously trying to find a way around this permission issue.

Thanks... :DYour question sounds strange...Why do you need users to have CREATE TABLE permissions? And creating temporary tables permission is transcendent from the default database security settings of TEMPDB and PUBLIC role...Unless whoever setup your business rules messed it up?|||I definitely need to clarify...I am writing stored procedures that require the use of temp tables or some sort of temporary storage for several result sets.

So I am investigating using table variables as another option because my stored procedures will not be giving my DBA's "blessing"...:)

He wants to eliminate all use of temp tables and he'll have a hissy fit if I have "create table" anywhere in my code. :) He also stated that the users are not allowed to create tables. I am not sure if that's how he set it up but that's what I was told.

I just want to make sure I won't have any issues with permissions he might have set up which is why I am looking at using table variables instead.

The "table" itself will have two columns and probably 100 rows (from the result sets).

My specific question would be, when initializing a table variable, is it treated just like any other variable?

Or is there potential that I can't declare a table variable if I am also not allowed to create temp tables?

Hope thats some what clearer...thanks :o|||2-column 100-row? Well, I guess your "DBA" doesn't trust your code ;)

We can trick your DBAby avoiding CREATE statements all together:
exec sp_addlogin 'test1', 'test1'
exec sp_adduser 'test1'
go
create table test (f1 int null, f2 int null)
insert test select 1,1 union select 2,2
go
create procedure sp_test2 as select * into #tmp from dbo.test; select * from #tmp
go
grant execute on sp_test2 to public
go
setuser 'test1'
exec sp_test2
setuser
go
drop procedure sp_test2
drop table test
go
exec sp_dropuser 'test1'
exec sp_droplogin 'test1'
go|||sigh...how I wish I could do something like this :) He will have to see this code as well and since there is a create table statement in it, I dont think it'll go through :)

I have a test database that I can run this on but I dont think it'll go to production.

Is it a bad idea to use a table variable to hold a two column 100 row set of data?|||Hey, if creating temp tables is the only thing your DBA is worried about, - I'd try to look for a replacement, or a better job. With memory tables (variable tables, SQL arrays, whatever) I can kill your server even faster, why bother with tempdb when you can get the brain of the machine much faster?
declare @.tbl table (col1 int null, col2 int null)
declare @.cnt int
select @.cnt = count(*) from @.tbl
while (case when (0>=@.cnt) then 1 else @.cnt end) >= 1 or /*should be AND */ @.cnt < 100 begin
insert @.tbl select (select count(*)-1 from @.tbl where col2-1 > col1-2),
(select count(*)+1 from @.tbl where col2+1 < col1+2)
select @.cnt = count(*) from @.tbl
end