Showing posts with label iterating. Show all posts
Showing posts with label iterating. Show all posts

Wednesday, March 28, 2012

Iterating updated fields within a Trigger

Hi,

I want to log updates to specific fields, storing the new and old
values. Is there any way I can iterate the collection of updated
fields within a trigger in order accomplish this?
Thanks in advance,

Julie Vazquezjv (julie_vazquez@.hotmail.com) writes:
> I want to log updates to specific fields, storing the new and old
> values. Is there any way I can iterate the collection of updated
> fields within a trigger in order accomplish this?

Not in a way that I would call painless. You could use something
with dynamic SQL, but that would come with a performance cost, and
you really don't want to spend to much time in triggers, since you
are in a transaction. You can easliy get into locking issues.

It is better - although boring - to type the SQL for each column to
log. Writing a program that generates the trigger code could be an
option to save time.

If you are in for massive auditing, consider using a third-party
product. A trigger-based solution is SQLAudit from Red Matrix.
Lumigent offers Entegra which works from the transaction log.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Do you know if there is away I could iterate through a table and get
all the field names? That would also be a great help.

Thanks.|||On 17 Jan 2005 17:02:45 -0800, jv wrote:

>Do you know if there is away I could iterate through a table and get
>all the field names? That would also be a great help.
>Thanks.

Hi jv,

Check out the INFORMATION_SCHEMA.COLUMNS view.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)|||jv (julie_vazquez@.hotmail.com) writes:
> Do you know if there is away I could iterate through a table and get
> all the field names? That would also be a great help.

Yes. But don't do it. If you are going roll your own, write code for
each column.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Iterating through tables

I want to create a stored procedure that will list all the tables for a
given database. I can get the databases using the sp_database function, but
the sp_tables requires that I be in database to use it. I want to create a
stored procedure that will take in the database name as a variable and
return a list of the tables for that database. I need to do this so as new
databases and tables are added and removed my program will have the up to
date data for the database. thanks.
JohnDECLARE @.dbname NVARCHAR(128), @.sql NVARCHAR(4000)
SET @.dbname = 'pubs'
SET @.sql =
'SELECT [table_name]
FROM '+QUOTENAME(@.dbname)+'.[information_schema].[tables]'
EXEC (@.sql)
David Portas
SQL Server MVP
--|||>> take in the database name as a variable and return a list of the tables f
or that database. <<
Have you considered that this is a METADATA problem and should be done
with sp_ routines and never in your own stored procedure? This will
lead to kludgfes with dynamic SQL, etc.
Gee, do you do that a lot? Do your users get to create tables, columns
or entire databases on the fly? Your whole approach sounds screwed up.
Would you like to expalin what you are doing so you can get a few
thousand dollars of freee consulting or do you just want a stinking
dirty little kludge?
No, your data dictionary should contain this kinfd of information, not
the programs.

Iterating through a recordset

I am a newly indoctrinated SQL Server 2005 user. So, forgive me if this
question is amatuer.
I am trying to create a stored proc that will read in a list of values
from one table (Possibly using a cte?) then iterate through each record
running another sp to populate a temp table and return all matching
records. The logic would be like the following:
///////
create procedure spSample1
@.column2 int
As
create table #tmptbl
(tmptblcol1 int not null)
with samplecte (var1 int)
As
(select column1 from table where column2 = @.column2)
begin
while not end-of-file
set localvar1 = current value of samplecte var1
insert into #tmptbl exec spSample2 localvar1
end
////////
This is a very rough outline. Hopefully it helps with clarification of
what I am trying to acheive. Any help would be greatly appreciated.
Thank you in advance!
rbrryankbrown@.gmail.com wrote:
> I am a newly indoctrinated SQL Server 2005 user. So, forgive me if this
> question is amatuer.
> I am trying to create a stored proc that will read in a list of values
> from one table (Possibly using a cte?) then iterate through each record
> running another sp to populate a temp table and return all matching
> records. The logic would be like the following:
> ///////
> create procedure spSample1
> @.column2 int
> As
> create table #tmptbl
> (tmptblcol1 int not null)
> with samplecte (var1 int)
> As
> (select column1 from table where column2 = @.column2)
> begin
> while not end-of-file
> set localvar1 = current value of samplecte var1
> insert into #tmptbl exec spSample2 localvar1
> end
> ////////
> This is a very rough outline. Hopefully it helps with clarification of
> what I am trying to acheive. Any help would be greatly appreciated.
> Thank you in advance!
> rbr
>
Something like the code below will do what you need, but I'd encourage
you to think "outside the box" and come up with a way to do this without
a temp table. Temp tables are usually unnecessary results of trying to
do "procedural" processes in a set-based environment like SQL.
DECLARE @.LoopVar
SELECT @.LoopVar = 0
SELECT TOP 1 @.LoopVar = <someincrementalID>
FROM table
WHERE <someincrementalID> > @.LoopVar
WHILE @.@.ROWCOUNT > 0
BEGIN
EXEC storedproc @.LoopVar
SELECT TOP 1 @.LoopVar = <someincrementalID>
FROM table
WHERE <someincrementalID> > @.LoopVar
END|||>> I am trying to create a stored proc that will read in a list of values from one tab
le (possibly using a CTE?) then iterate through each record [sic] running another sp t
o populate a temp table and return all matching records [sic]. The logic would be
l
ike the following: <<
You are still thinking of a file system - a magnetic tape file
system, to be more precise, doing a merge from a scratch tape.
Let's get back to the basics of an RDBMS. Rows are not records; fields
are not columns; tables are not files; there is no sequential access or
ordering in an RDBMS, so "first", "next" and "last" are totally
meaningless.
SQL is a declarative set-oriented language. We tend to write one
statement to do a job; if I want to insert data into a table, I write
something like:
INSERT INTO Foobar (a, b, c, ..)
SELECT x, y, z, ..
FROM Floob, Snarf, ..
WHERE ..;
One statement, no loops, no if-then control flow. You tell SQL
**what** you want, not **how** to do it. You want to write code as if
you were still in a 3GL procedural language. And I'll bet that your
schema is full of redundancies, too.
Nope, it was too vague to be usable for any concrete suggestions. But
it did demonstrate that you missed the very foundations of RDBMS. Get
a few books (insert shameless plug for my stuff here), clear out
everything you already know and start over. "To drink new tea, you
must first empty the old tea from your cup" - Zen proverb.|||Trust the CELKO grasshopper... He is a curmudgeon and will not hesitate to
tell you what he thinks about a subject but search on his previous posts and
you will learn. Hang around here and lurk. Search is your friend... Good
luck.
"ryankbrown@.gmail.com" wrote:

> I am a newly indoctrinated SQL Server 2005 user. So, forgive me if this
> question is amatuer.
> I am trying to create a stored proc that will read in a list of values
> from one table (Possibly using a cte?) then iterate through each record
> running another sp to populate a temp table and return all matching
> records. The logic would be like the following:
> ///////
> create procedure spSample1
> @.column2 int
> As
> create table #tmptbl
> (tmptblcol1 int not null)
> with samplecte (var1 int)
> As
> (select column1 from table where column2 = @.column2)
> begin
> while not end-of-file
> set localvar1 = current value of samplecte var1
> insert into #tmptbl exec spSample2 localvar1
> end
> ////////
> This is a very rough outline. Hopefully it helps with clarification of
> what I am trying to acheive. Any help would be greatly appreciated.
> Thank you in advance!
> rbr
>|||I trust that you are all correct. As I stated, I am new to this from a
much more procedural background. As most of us do when faced with a new
problem we fall back on what we know best.
I already wrote a simple sp that does the job just fine. I was asked by
a superior to find a way to do it reusing another pre-existing sp to
acheive the same result. This is what spawned my question.
Thanks for imparting your wisdom upon me!
rbr
StvJston wrote:
> Trust the CELKO grasshopper... He is a curmudgeon and will not hesitate to
> tell you what he thinks about a subject but search on his previous posts a
nd
> you will learn. Hang around here and lurk. Search is your friend... Goo
d
> luck.
> "ryankbrown@.gmail.com" wrote:
>|||Oh, and by the way...
All is fine with my db. No redundancies here. The only complaint i've
heard from my programmers is that it may be over-normalized.
But thanks for the unproductive insult anyway.
rbr
rbr wrote:
> I trust that you are all correct. As I stated, I am new to this from a
> much more procedural background. As most of us do when faced with a new
> problem we fall back on what we know best.
> I already wrote a simple sp that does the job just fine. I was asked by
> a superior to find a way to do it reusing another pre-existing sp to
> acheive the same result. This is what spawned my question.
> Thanks for imparting your wisdom upon me!
> rbr
> StvJston wrote:

iterating through a multi-value parameter

is there a way to loop through a multi-value parameter and retrieve contents
of all selected values to dislpay on a report. currently, i can reference
individual parameter values by using Parameters!PARAMETER.Value(0...).
However with dynamic parameter values generated from a query, this becomes a
pain
regardsnitz
Create a text box and put in the following:
=Join(Parameters!MyParam.Label, ", ")
This should grab all of the parameters selected and display them in your
textbox. You also have to add the parameter to the query as well; but it
sounds like you have already done that. For example:
and (m.company_num IN (@.MyParam))
Hope this helps.
Rob Cuscaden
"nitz" wrote:
> is there a way to loop through a multi-value parameter and retrieve contents
> of all selected values to dislpay on a report. currently, i can reference
> individual parameter values by using Parameters!PARAMETER.Value(0...).
> However with dynamic parameter values generated from a query, this becomes a
> pain
> regards
>|||duh...thank you very much..i really need to review what text functions are
available..
since i have you on the line ;)..is there a way to loop through the
parameters collections grab paramaters and their values and output to a text
box...it would be nice to have reusable bit of code for every report.
instead of this
= "DIVISION: " & Parameters!DIVISION.Value & " BOUTIQUES: " &
JOIN(Parameters!REPS.Label, ", ") & VbCrLf & etc
thanks again
"Rob" wrote:
> nitz
> Create a text box and put in the following:
> =Join(Parameters!MyParam.Label, ", ")
> This should grab all of the parameters selected and display them in your
> textbox. You also have to add the parameter to the query as well; but it
> sounds like you have already done that. For example:
> and (m.company_num IN (@.MyParam))
> Hope this helps.
> Rob Cuscaden
> "nitz" wrote:
> > is there a way to loop through a multi-value parameter and retrieve contents
> > of all selected values to dislpay on a report. currently, i can reference
> > individual parameter values by using Parameters!PARAMETER.Value(0...).
> > However with dynamic parameter values generated from a query, this becomes a
> > pain
> > regards
> >sql

Iterating the rows of a script component

Hi There,

Can someone please let me know what is the best way to iterate the output rows of a script component and stick in those ids in a where clause of a select query (to retrieve additional info from a database)? Is this possible at all? If not, what is the best way to deal with this situation?

Thanks a lot!!

Sam_res03 wrote:

Hi There,

Can someone please let me know what is the best way to iterate the output rows of a script component and stick in those ids in a where clause of a select query (to retrieve additional info from a database)? Is this possible at all? If not, what is the best way to deal with this situation?

Thanks a lot!!

Any particular reason you aren't posting in your other thread on this topic? This is a dup.|||

No particular reason. I just thought I wasn't specific about my problem. I didn't know how to modify the title. I am in pressure to finish off a task and didn't take time to explore how i could modify the title of the post.

Thanks.

|||

Sam_res03 wrote:

Hi There,

Can someone please let me know what is the best way to iterate the output rows of a script component and stick in those ids in a where clause of a select query (to retrieve additional info from a database)? Is this possible at all? If not, what is the best way to deal with this situation?

Thanks a lot!!

Probably the best way is to push the data into recordset destination and then loop over them with a ForEach loop.

Hopefully this will help:

Execute SQL Task into an object variable - Shred it with a Foreach loop
(http://blogs.conchango.com/jamiethomson/archive/2005/07/04/SSIS-Nugget_3A00_-Execute-SQL-Task-into-an-object-variable-_2D00_-Shred-it-with-a-Foreach-loop.aspx)

In this example the recordset is populated by an Execute SQL Task rather than a recordsset destinaiton, but teh principle thereafter is the same I think.

-Jamie

|||

Jamie,

Thanks a lot for your reply. I think I am almost there...So I have one one column in the recordset. Is this what I should do?

By using a for each loop container, with foreach ado enumerator I can get the id and store in a variable.

Next can I have a dataflow task in the foreach loop...

and in that dataflow task, can I access this id pass it as a variable to an sql command (in the ole db source). The sql command is a select statement with a where clause.I am unable to figure out how that can be done.

I really appreciate your response....

Thanks a lot!!

|||

Sam_res03 wrote:

Jamie,

Thanks a lot for your reply. I think I am almost there...So I have one one column in the recordset. Is this what I should do?

By using a for each loop container, with foreach ado enumerator I can get the id and store in a variable.

Next can I have a dataflow task in the foreach loop...

and in that dataflow task, can I access this id pass it as a variable to an sql command (in the ole db source). The sql command is a select statement with a where clause.I am unable to figure out how that can be done.

I really appreciate your response....

Thanks a lot!!

You're right so far. You have two options to use your value in your SQL statement:

1) Use '?' parameters

2) Use expressions. Some info here: http://blogs.conchango.com/jamiethomson/archive/2005/12/09/SSIS_3A00_-Using-dynamic-SQL-in-an-OLE-DB-Source-component.aspx

-Jamie

|||

Hi Jamie,

I took your approach and I get this error..

Error: ForEach Variable Mapping number 1 to variable cannot be applied.

I tried to use a foreach loop container. I mapped index 0 to a user variable of type integer. IN the script component I have the row object cast as an integer.

I also get this error..

Error: The type of the value being assigned to variable "User::ID" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.

Do you see what mistake I am doing? Can you please reply?

Thanks.

|||

What's the SQL statement? What's the type of the first column?

-Jamie

|||

Type of the first column is Integer.

For some reason, if I try to map it to a variable of type integer, it gives me an error. But if I try to map it to a variable of type object it doesn't complain. And with the help of msgbox scrript component i am able to see the ids. But the problem I am getting to is..if the variable is of type object, how can I create an expression with that object (in the where clause)and use it as a variable for sql command with variable.

Are you getting my problem? Please reply.

Thanks a lot for bearing with me.

|||

Sam_res03 wrote:

Type of the first column is Integer.

For some reason, if I try to map it to a variable of type integer, it gives me an error. But if I try to map it to a variable of type object it doesn't complain. And with the help of msgbox scrript component i am able to see the ids. But the problem I am getting to is..if the variable is of type object, how can I create an expression with that object (in the where clause)and use it as a variable for sql command with variable.

Are you getting my problem? Please reply.

Thanks a lot for bearing with me.

Hi Sam,

You can set up another variable which uses expressions that casts the object variable to an integer variable (e.g., the expression would look like "<DT_I4> @.[User::objvariable]" for an 4-byte signed integer which maps to the standard Int32 integer). Then you can use that in your where clause.

I do have a question... what is the exact type of your integer column? If it's bigint, it really won't work with a standard Integer type... and you have to use <DT_I8> instead.|||

Jon Limjap wrote:

Sam_res03 wrote:

Type of the first column is Integer.

For some reason, if I try to map it to a variable of type integer, it gives me an error. But if I try to map it to a variable of type object it doesn't complain. And with the help of msgbox scrript component i am able to see the ids. But the problem I am getting to is..if the variable is of type object, how can I create an expression with that object (in the where clause)and use it as a variable for sql command with variable.

Are you getting my problem? Please reply.

Thanks a lot for bearing with me.

Hi Sam,

You can set up another variable which uses expressions that casts the object variable to an integer variable (e.g., the expression would look like "<DT_I4> @.[User::objvariable]" for an 4-byte signed integer which maps to the standard Int32 integer). Then you can use that in your where clause.

I do have a question... what is the exact type of your integer column? If it's bigint, it really won't work with a standard Integer type... and you have to use <DT_I8> instead.

Yeah, Jon is right. If I were you I would investigate why you can't store it in an Integer variable. Try Int16, Int32 and Int64.

-Jamie

|||

Thanks Jamie. I will investigate it. By the way, if I have a dataflow task(source and the destination) in a foreach loop container and if the number of items it is iterating is x items , does it mean that it will make x calls to the source database?

Thanks.

|||

Thanks Jon. I really appreciate your response. I will try your trick and will let you know soon.

|||

Sam_res03 wrote:

Thanks Jamie. I will investigate it. By the way, if I have a dataflow task(source and the destination) in a foreach loop container and if the number of items it is iterating is x items , does it mean that it will make x calls to the source database?

Yes.

|||

Then that is not an efficient way either making 1200 calls. The only reason i tried to do this was, I didn't want to scan a table with 600k rows and do a look up in a temp table for the needed 1200 rows. I wanted to scan only for these 1200 rows/ids. Is there a way to achieve this? If I have the ids in an object, would i be able to concatenate those ids into a string varible and use that sring variable in a where clause of a select statement? I am getting this data from an Oracle DB. Is there any limitation on using a where clause in a select statement. If so, what is the other to solve this problem?

Thanks Jamie. I really appreciate your response. Thanks a lot!!

Iterating Data Movement

Hi all

I am wanting to continuously monitor a source table throughout the day and as data becomes available, process it and insert it into one of a number of tables.

I have tried achieving this using a FOR LOOP and setting the halt condition such that it is not stisfiable. However, this has a couple of problems:

1) It runs in a tight loop and consequently degrades system performance enormously.

2) I can't get transactions to work. I would like each iteration of the loop to spawn a new transaction under which the tasks in the loop can run. Therefore, if one of the tasks fails during such an iteration, only the updates affected by that iteration are lost.

Ideally, I would like to be able to put a wait statement within the loop container so that it runs every couple of seconds. And would also like to implement transactions as described above.

All help is appreciated.

Jays :-)

I think you want a trigger on the table so you know when the data in that table is changed, and copy it accordingly.

|||

Hi Christian

I would like to avoid that if I can.

My package has custom logging that writes a set of audit records each time it is run. This would result in a LOT of these records being written throughout the day when one such set would do.

Thanks

Jays :-)

Iterating

Hi,

I have a 6 different textboxes in my web application. I have 6 different tables in my database such as tbl1,tbl2,tbl3 etc.

When the user clicks the submit button I have to check whether the values in the textboxes match the value in the database. (if in txt1 the user enters 3 I need to go to tbl1 and check if there is such a value).

What is the most efficient way to perform such a check? Will I need to write 6 select statements or can I use a loop and if I can use a loop I would appreciate an example

Thanks

Why do you need to do this?

If you are loading the value into the textbox to begin with, and you simply want to check whether the text has changed, there is a text changed event raised by the textbox. If that doesn't work for you, you could always store the initial value of the textbox in a custom property when you populate the textbox:

myTextBox1.Attributes.Add('initValue',myValue)

Then simply check to see whether it has changed.

If you MUST check these against values from the database (i.e.; for data volatility reasons), you should retrieve all six from the db in a single SQL query, if possible. It would be more efficient...

|||

I need to do this coz the user enters a code in the textboxes and I need to check if such a code exists in the db and if there is no such a code then the user must select another code.

How can I retrieve all 6 values in a single query from 6 different tables?