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

Friday, March 23, 2012

R/W access problem with var in script task

Hi there

I have a a global variable for the package.

Now in script task of data flow I am trying to assign some value to it.

I have also defined same variable in Read/Write property of script task.

But at run time it gives me following error:

"The collection of variables locked for read and write access is not available outside of PostExecute."

How do we resolve this?

Thanks and Regards

Rahul Kumar

And in which overrided Sub are you trying to access this variable?

-Tom

|||

Hi Tom

I am doing all this in

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

|||

Try this:

Public Overloads Overrides Sub ProcessInput(ByVal inputID As Integer, ByVal buffer As PipelineBuffer)
If Buffer Is Nothing Then
Throw New ArgumentNullException("buffer")
End If
Dim vars As IDTSVariables90
Dim varServerName As IDTSVariable90
Dim varPackageName As IDTSVariable90

....

Try

...
Me.VariableDispenser.LockForRead("ServerName")

...
Me.VariableDispenser.GetVariables(vars)
varServerName = vars.Item(0) (0 because this was the first variable you've locked)

...

catch ....

|||

You can find lots of examples by just typing in msnsearch "using variable in script component"

like for example:

http://msdn2.microsoft.com/en-us/library/aa337079.aspx

-Tom

|||

Tom, your code sample is not actually for a Script Component, rather a full custom component.

Rahul, to be clear, when using the variable lock lists provide in the UI of the the Script Component you cannot access variables in the process input member (MyBuffer_ProcessInputRow). You can access read-only variables in the PreExecute and read-write variables in PostExecute

From the BOL link in Tom's second post -

Important:

The collection of ReadWriteVariables is only available in the PostExecute method to maximize performance and minimize the risk of locking conflicts.

If you do the locking yourself though you get more control. Try this sample code, but create the variables first if you try and run it.

Public Class ScriptMain

Inherits UserComponent

Private counter As Integer

Public Overrides Sub PreExecute()

' Initialise Counter from RO variable

counter = Me.Variables.VariableRO

MyBase.PreExecute()

End Sub

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

' Increment Counter

counter = counter + 1

' Use manual locking to set variable in Process Input

Dim variables As IDTSVariables90

Me.VariableDispenser.LockOneForRead("VariableRWManual", variables)

variables(0).Value = 125

variables.Unlock()

End Sub

Public Overrides Sub PostExecute()

' Store updated counter in RW variable

Me.Variables.VariableRW = counter

' Set a value for testing using manual locking

Dim variables As IDTSVariables90

Me.VariableDispenser.LockOneForRead("Variable", variables)

variables(0).Value = 5

variables.Unlock()

MyBase.PostExecute()

End Sub

End Class

|||

Hi Darren

Great Help!! thanks,

I got my work done.

Regards

Rahul Kumar,Software Engineer,India

sql

R/W access problem with var in script Component

Hi there

I have a a global variable for the package.

Now in script task of data flow I am trying to assign some value to it.

I have also defined same variable in Read/Write property of script task.

But at run time it gives me following error:

"The collection of variables locked for read and write access is not available outside of PostExecute."

How do we resolve this?

Thanks and Regards

Rahul Kumar

And in which overrided Sub are you trying to access this variable?

-Tom

|||

Hi Tom

I am doing all this in

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

|||

Try this:

Public Overloads Overrides Sub ProcessInput(ByVal inputID As Integer, ByVal buffer As PipelineBuffer)
If Buffer Is Nothing Then
Throw New ArgumentNullException("buffer")
End If
Dim vars As IDTSVariables90
Dim varServerName As IDTSVariable90
Dim varPackageName As IDTSVariable90

....

Try

...
Me.VariableDispenser.LockForRead("ServerName")

...
Me.VariableDispenser.GetVariables(vars)
varServerName = vars.Item(0) (0 because this was the first variable you've locked)

...

catch ....

|||

You can find lots of examples by just typing in msnsearch "using variable in script component"

like for example:

http://msdn2.microsoft.com/en-us/library/aa337079.aspx

-Tom

|||

Tom, your code sample is not actually for a Script Component, rather a full custom component.

Rahul, to be clear, when using the variable lock lists provide in the UI of the the Script Component you cannot access variables in the process input member (MyBuffer_ProcessInputRow). You can access read-only variables in the PreExecute and read-write variables in PostExecute

From the BOL link in Tom's second post -

Important:

The collection of ReadWriteVariables is only available in the PostExecute method to maximize performance and minimize the risk of locking conflicts.

If you do the locking yourself though you get more control. Try this sample code, but create the variables first if you try and run it.

Public Class ScriptMain

Inherits UserComponent

Private counter As Integer

Public Overrides Sub PreExecute()

' Initialise Counter from RO variable

counter = Me.Variables.VariableRO

MyBase.PreExecute()

End Sub

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

' Increment Counter

counter = counter + 1

' Use manual locking to set variable in Process Input

Dim variables As IDTSVariables90

Me.VariableDispenser.LockOneForRead("VariableRWManual", variables)

variables(0).Value = 125

variables.Unlock()

End Sub

Public Overrides Sub PostExecute()

' Store updated counter in RW variable

Me.Variables.VariableRW = counter

' Set a value for testing using manual locking

Dim variables As IDTSVariables90

Me.VariableDispenser.LockOneForRead("Variable", variables)

variables(0).Value = 5

variables.Unlock()

MyBase.PostExecute()

End Sub

End Class

|||

Hi Darren

Great Help!! thanks,

I got my work done.

Regards

Rahul Kumar,Software Engineer,India

Wednesday, March 21, 2012

Quotes within a query

Help, please.

I'm going crazy trying to figure out how to form this SQL query. I am querying an Informix linked server and I need to pass a variable date. I am using an expression to create the query like so

"Select count(*) from " + @.[User::varDBName] + ":informix.doc_tl WHERE " + @.[User::varDBName] +":informix.doc_tl.d_received = {D " + @.[User::varDate] +"} "

The informix query needs the date to be {D "2007-01-15"} but for the life of me, I can't get the date enclosed in quotes.

The error I get is

An OLE DB record is available. Source: "(null)". HResult: 0x80040E14

Description: "(null)"

Can anyone tell me what I'm doing wrong?

Thanks

Hi Chris,

Can you try using single quote character (') or escaped quote character i.e. using quote twice ("")?

-- Waseem

|||

Hi!

Thanks for the reply. I thought I tried both but upon retry, the single quote character worked. I do get a code page warning but I think that is OK.

Thanks so much!

Tuesday, March 20, 2012

Quicker Cursor or Table Variable

Hi
I have a large update batch to make on our database which will run overnight
when no users are logged in.
I always use a Table Variable instead of a Cursor to conserve resources, but
in this case resources are not a problem but speed is.
Which would be quicker: cursor or table variable, also would I get a
performance benefit from running the batch within a Stored Procedure rather
than Query Analyser.
Thanks
BHave you looked at the execution plan used in your batch update. This should
pinpoint where the problem is.
Use a binary approach to this. In your batch write print statements which
will display datediff statements throughout the batch. This way you will
know which portion takes the longest.
I think you will find that local table variables with indexes (primary key
constraint) offer the best performance. You will probably also find that
using one or more stored procedures offers better performance as well.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Ben" <Ben@.Newsgroups.microsoft.com> wrote in message
news:uz1niRiAGHA.916@.TK2MSFTNGP10.phx.gbl...
> Hi
> I have a large update batch to make on our database which will run
> overnight
> when no users are logged in.
> I always use a Table Variable instead of a Cursor to conserve resources,
> but
> in this case resources are not a problem but speed is.
> Which would be quicker: cursor or table variable, also would I get a
> performance benefit from running the batch within a Stored Procedure
> rather
> than Query Analyser.
> Thanks
> B
>|||Ben
I'd understan you if you ask what is a difference between a table variable
and a temporary table?
How does it relate to the cursors?
Try to avoid using cursors because it may hurt a performance , insead use
SET BASED process to update a table
If you show us what you are trying to accomplish , we van suggest something
more useful.
"Ben" <Ben@.Newsgroups.microsoft.com> wrote in message
news:uz1niRiAGHA.916@.TK2MSFTNGP10.phx.gbl...
> Hi
> I have a large update batch to make on our database which will run
> overnight
> when no users are logged in.
> I always use a Table Variable instead of a Cursor to conserve resources,
> but
> in this case resources are not a problem but speed is.
> Which would be quicker: cursor or table variable, also would I get a
> performance benefit from running the batch within a Stored Procedure
> rather
> than Query Analyser.
> Thanks
> B
>

Monday, March 12, 2012

Quick SQL Question?

Hi Everyone:

I am trying to create the following SP, but get an Error stating "Must Declare the scalar variable "@.LoanApplicationID" eventhough I think I am declaring it. I am not sure what am I doing incorrect here, but a prompt solution would be appreciated. Thanks.

Here is the code:

CREATE PROCEDURE [LAFProcess].[uspUpdateProcessList]

AS

DECLARE @.LoanApplicationID int

SET NOCOUNT ON

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SET @.LoanApplicationID = (SELECT TOP 1 LoanApplicationID FROM LAFPRocess.WorkList WHERE Status = 2)

INSERT INTO LAFProcess.ProcessList (WorkListID, CreatedOn, ProcessStartedOn, Status)

SELECT TOP 1 WorkListID, GetDate(), NULL, 2

FROM LAFProcess.WorkList

WHERE Status = 2;

GO

UPDATE LAFProcess.WorkList

SET Status = 1

WHERE LoanApplicationID = @.LoanApplicationID

GO

GRANT EXEC ON [LAFProcess].[uspUpdateProcessList] TO PUBLIC

GO

The "GO" keyword causes this error. More information can be found at:

http://searchsqlserver.techtarget.com/tip/1,289483,sid87_gci1161826,00.html

|||Thanks.

Friday, March 9, 2012

Quick question: how do I get the name of the current database?

Is there an sp_zzzzzz function to return the name of the current database?
I would like to use this name as a variable in a stored procedure in order
to create names for further databases (by appending a tag, such as
MYDATABASE_BLOB001, ..._BLOB002 etc.

Thanks.SELECT DB_NAME()

Why create a database from a Stored Procedure?
--
David Portas
SQL Server MVP
--|||"Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
message news:ct5isn$9m3$1$8302bc10@.news.demon.co.uk...
> Is there an sp_zzzzzz function to return the name of the current database?
> I would like to use this name as a variable in a stored procedure in order
> to create names for further databases (by appending a tag, such as
> MYDATABASE_BLOB001, ..._BLOB002 etc.
>
> Thanks.

select db_name()

It might be worth considering why you need multiple databases - could
BLOB001 be part of a key in a table instead? This discussion applies to
tables, not databases, but the principle is exactly the same:

http://www.sommarskog.se/dynamic_sql.html#Sales_yymm

Simon|||The issue is that we have one database with our adjacency list in and then
one or more with our media blobs in. Our users won't be using full SQL
server, so we have 2Gb limit on MSDE. When one of the blob databases
reaches close to 2Gb, we rollover to the next blob database. In my
adjacency table I store the blob database ID and Key for the blob associated
with each node. I also have a table with all of the blob databases listed,
so I can lookup and find which database the ID refers to.

Can't help it. Customers are cheapskates ;)

"Simon Hayes" <sql@.hayes.ch> wrote in message
news:41f65413$1_3@.news.bluewin.ch...
> "Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
> message news:ct5isn$9m3$1$8302bc10@.news.demon.co.uk...
>> Is there an sp_zzzzzz function to return the name of the current
>> database? I would like to use this name as a variable in a stored
>> procedure in order to create names for further databases (by appending a
>> tag, such as MYDATABASE_BLOB001, ..._BLOB002 etc.
>>
>>
>> Thanks.
>>
>>
>>
> select db_name()
> It might be worth considering why you need multiple databases - could
> BLOB001 be part of a key in a table instead? This discussion applies to
> tables, not databases, but the principle is exactly the same:
> http://www.sommarskog.se/dynamic_sql.html#Sales_yymm
> Simon|||"Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
message news:ct5l4s$ig$1$8300dec7@.news.demon.co.uk...
> The issue is that we have one database with our adjacency list in and then
> one or more with our media blobs in. Our users won't be using full SQL
> server, so we have 2Gb limit on MSDE. When one of the blob databases
> reaches close to 2Gb, we rollover to the next blob database. In my
> adjacency table I store the blob database ID and Key for the blob
associated
> with each node. I also have a table with all of the blob databases
listed,
> so I can lookup and find which database the ID refers to.
> Can't help it. Customers are cheapskates ;)

Something to be aware of: I think that MSDE may also limit you on the number
of databases you are allowed so there may be a limit on the number of times
you can "rollover" to another database.

Brian.

www.cryer.co.uk/brian