Wednesday, March 28, 2012
LOOPING UPDATE
I just require a bit of guidance on a SQL query I am writing. I am updating
a table with values, and the first field is a TYPE field, I set this to A
and then populate fields 2-15 with a variety of default updates and values
from other tables, I then do a second insert whreeby I set the same TYPE
field to B and update fields 2-5 and then the unique fields 16-20.
At the moment my second update has two issues;
1. It sets EVERY type to B (although it correctly doubles the amount of
entries in the table)
2. The B entries are appended to the bottom of the table, ideally I want the
table structure to be ABABABAB etc
Any help to a SQL newbie appreciated!> 1. It sets EVERY type to B (although it correctly doubles the amount of
> entries in the table)
Sounds like your WHERE clause may be at fault. Could you post a CREATE TABLE
statement and the UPDATE/INSERT statements so that we can reproduce your
problem.
> 2. The B entries are appended to the bottom of the table, ideally I want
the
> table structure to be ABABABAB etc
Tables have no logical ordering. If you want to see the results in a
particular order then use ORDER BY on a SELECT statement when you query the
table. A clustered index orders data in physical storage but not necessarily
when you query the table.
David Portas
SQL Server MVP
--|||Hi David
I have rewritten the part of the routine with a separate WHERE clause on the
type field at the end of the update and this seems to work so thank you.
I have several more loops to write, I will then try the Order by at the end
of the routine.
Thank you very very much for your quick and informative response, it really
is appreciated.
Steve
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:8ZKdncKsYuPgDrHdRVn-uA@.giganews.com...
> Sounds like your WHERE clause may be at fault. Could you post a CREATE
TABLE
> statement and the UPDATE/INSERT statements so that we can reproduce your
> problem.
>
> the
> Tables have no logical ordering. If you want to see the results in a
> particular order then use ORDER BY on a SELECT statement when you query
the
> table. A clustered index orders data in physical storage but not
necessarily
> when you query the table.
> --
> David Portas
> SQL Server MVP
> --
>sql
LOOPING UPDATE
I just require a bit of guidance on a SQL query I am writing. I am updating
a table with values, and the first field is a TYPE field, I set this to A
and then populate fields 2-15 with a variety of default updates and values
from other tables, I then do a second insert whreeby I set the same TYPE
field to B and update fields 2-5 and then the unique fields 16-20.
At the moment my second update has two issues;
1. It sets EVERY type to B (although it correctly doubles the amount of
entries in the table)
2. The B entries are appended to the bottom of the table, ideally I want the
table structure to be ABABABAB etc
Any help to a SQL newbie appreciated!> 1. It sets EVERY type to B (although it correctly doubles the amount of
> entries in the table)
Sounds like your WHERE clause may be at fault. Could you post a CREATE TABLE
statement and the UPDATE/INSERT statements so that we can reproduce your
problem.
> 2. The B entries are appended to the bottom of the table, ideally I want
the
> table structure to be ABABABAB etc
Tables have no logical ordering. If you want to see the results in a
particular order then use ORDER BY on a SELECT statement when you query the
table. A clustered index orders data in physical storage but not necessarily
when you query the table.
--
David Portas
SQL Server MVP
--|||Hi David
I have rewritten the part of the routine with a separate WHERE clause on the
type field at the end of the update and this seems to work so thank you.
I have several more loops to write, I will then try the Order by at the end
of the routine.
Thank you very very much for your quick and informative response, it really
is appreciated.
Steve
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:8ZKdncKsYuPgDrHdRVn-uA@.giganews.com...
> > 1. It sets EVERY type to B (although it correctly doubles the amount of
> > entries in the table)
> Sounds like your WHERE clause may be at fault. Could you post a CREATE
TABLE
> statement and the UPDATE/INSERT statements so that we can reproduce your
> problem.
> > 2. The B entries are appended to the bottom of the table, ideally I want
> the
> > table structure to be ABABABAB etc
> Tables have no logical ordering. If you want to see the results in a
> particular order then use ORDER BY on a SELECT statement when you query
the
> table. A clustered index orders data in physical storage but not
necessarily
> when you query the table.
> --
> David Portas
> SQL Server MVP
> --
>
Wednesday, March 21, 2012
loop through a tables columns
i want to build a long string with this type of logic
foreach (column in table.columns)
{
strValues += column.name + "=" + row.value
}
thank you for your helpAbraham,
Can you give me some sample data and desired results to work with?
Not exactly sure what you're asking for.
HTH
Jerry
"Abraham Andres Luna" <abe@.rdk.com> wrote in message
news:e8wfGkB0FHA.2460@.TK2MSFTNGP10.phx.gbl...
> is there a way using t-sql to loop through a tables columns?
> i want to build a long string with this type of logic
> foreach (column in table.columns)
> {
> strValues += column.name + "=" + row.value
> }
> thank you for your help
>|||What are you trying to accomplish? Is this supposed to be some kind of
data interchange format? If so, how about using SELECT ... FOR XML to
generate an XML fragment instead?
If you really want to do this dynamically then take a look at this proc
for inspiration:
http://vyaskn.tripod.com/code/generate_inserts.txt
David Portas
SQL Server MVP
--|||Abraham Andres Luna wrote:
> is there a way using t-sql to loop through a tables columns?
> i want to build a long string with this type of logic
> foreach (column in table.columns)
> {
> strValues += column.name + "=" + row.value
> }
>
No, you will need to use a client tool for this. The best you can do in
T-SQL is (air code):
select 'col1=' + cast(col1 as varchar) + ... + '; colN=' + colN from table
I know you want to avoid this exercise, but the only way would be to use a
client tool: for example: an ADO Recordset.
Oh, I suppose you could go to a great deal of trouble to create a dynamic
sql statement by looping through the result of querying the
information_schema.columns view, but I would not advise this.
Bob Barrows
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||ty for the answer, that proc was a big help
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1129225553.008794.38510@.g14g2000cwa.googlegroups.com...
> What are you trying to accomplish? Is this supposed to be some kind of
> data interchange format? If so, how about using SELECT ... FOR XML to
> generate an XML fragment instead?
> If you really want to do this dynamically then take a look at this proc
> for inspiration:
> http://vyaskn.tripod.com/code/generate_inserts.txt
> --
> David Portas
> SQL Server MVP
> --
>
Monday, March 12, 2012
Lookup current id in Kimball Type II dimension for fact rows
Hi all,
Maybe someone here can help me out: I have a Kimball type II dimension, where i track changes in a hierarchy. Each row has a RowStartDate and RowEndDate property to indicate from when to when a certain row should be used.
Now i want to load facts to that table. So each fact will have a certain date associated with it that i can use to lookup the right Id (a certain SourceId can have mulitiple integer Ids when there are historic changes) and then load the facts.
Is there a building block I can use for that? I could do this with SQL scripts but the client would prefer to have as much as possible done in SSIS. The Lookup transformation will only let me specify an equal (inner join where A=B) join, but i need equal for one column (SourceId) and then >= and <= (RowStart and RowEnd) to find the right row version.
Any ideas? Thanks in advance,
John
That question have been asked before. There are several works arround to that problem, here is one:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=406485&SiteID=1
You may want to try a search with 'SSIS range lookup'
|||That's it, excellent! I was googling on the wrong words.
Thanks,
John
Lookup cachetype = none question
HI, I use a lookup transform on one of my dataflow. My data look a bit like this (the actual data is more complicated, kit is a combination of type 1 - description and type 2 - code):
ID Code Description
-- -
1 AAA PRODUCT1
2 AAA PRODUCT2
3 AAA PRODUCT3
4 BBB PRODUCT4
The problem is simple: I would like to insert ID 1 and update it with subsequent rows that have the same code.
If the lookup transform finds a match, the row is updated, else, the row is inserted (using the error path of the lookup) via an OLE_DB command. The lookup cachetype is set to none. My problem is all rows are inserted. But if I use a second lookup that gives me the ID using the Code column, the second lookup sees the inserted data.
My question is why the second lookup is able to find out the inserted data while the first one cannot? The SCD wizard cannot resolve this either. I resolved this by using an asynchronous script component and manage the logic in there. But, still, a lookup with no cache (roud trip to the DB every time) should be able to do the job.
Thank you,
Ccote
Generally this is because the dataflow works on buffers not rows. All the rows in a buffer are processed by a component before the rows are passed on to the next component. So the 1st lookup doesn't find the data because it most likely hasn't actually been inserted yet, while the 2nd one finds it because the data has actually been inserted.
HTH,
Matt
Wednesday, March 7, 2012
Looking for SQL-Server documenter
I'm looking for a documenter for the SQL-Server. It should describe the
tables, views with its structure (fieldnames, type, length, etc.) and
equally important it should be possible to export this information in a file
(XML, CSV, whatever).
Does anybody knows a tool like that and if so, where to get it?
Thx in advance
MichaelTry SQl Scribe Docuemtnation Builder
http://www.ag-software.com
--
kind regards
Greg O
Need to document your databases. Use the first and still the best AGS SQL
Scribe
http://www.ag-software.com
"Michael Bender" <technik@.salescom.de> wrote in message
news:df69m7$kjp$03$1@.news.t-online.com...
> Hi everybody,
> I'm looking for a documenter for the SQL-Server. It should describe the
> tables, views with its structure (fieldnames, type, length, etc.) and
> equally important it should be possible to export this information in a
> file
> (XML, CSV, whatever).
> Does anybody knows a tool like that and if so, where to get it?
> Thx in advance
> Michael
>|||Michael Bender wrote:
> Hi everybody,
> I'm looking for a documenter for the SQL-Server. It should describe the
> tables, views with its structure (fieldnames, type, length, etc.) and
> equally important it should be possible to export this information in a file
> (XML, CSV, whatever).
> Does anybody knows a tool like that and if so, where to get it?
> Thx in advance
> Michael
SchemaToDoc (http://www.schematodoc.com) will produce a Word document
that includes information about the tables and fields in your database.
The program documents: Primary Keys, Field Information (type, size,
defaults, nullable), Indexes, Check Constraints, Descriptions/Comments,
Foreign Keys, and Triggers. The program also lets you annotate your
tables and fields, and include those comments in the Word doc.
Looking for SQL-Server documenter
I'm looking for a documenter for the SQL-Server. It should describe the
tables, views with its structure (fieldnames, type, length, etc.) and
equally important it should be possible to export this information in a file
(XML, CSV, whatever).
Does anybody knows a tool like that and if so, where to get it?
Thx in advance
Michael
Try SQl Scribe Docuemtnation Builder
http://www.ag-software.com
kind regards
Greg O
Need to document your databases. Use the first and still the best AGS SQL
Scribe
http://www.ag-software.com
"Michael Bender" <technik@.salescom.de> wrote in message
news:df69m7$kjp$03$1@.news.t-online.com...
> Hi everybody,
> I'm looking for a documenter for the SQL-Server. It should describe the
> tables, views with its structure (fieldnames, type, length, etc.) and
> equally important it should be possible to export this information in a
> file
> (XML, CSV, whatever).
> Does anybody knows a tool like that and if so, where to get it?
> Thx in advance
> Michael
>
|||Michael Bender wrote:
> Hi everybody,
> I'm looking for a documenter for the SQL-Server. It should describe the
> tables, views with its structure (fieldnames, type, length, etc.) and
> equally important it should be possible to export this information in a file
> (XML, CSV, whatever).
> Does anybody knows a tool like that and if so, where to get it?
> Thx in advance
> Michael
SchemaToDoc (http://www.schematodoc.com) will produce a Word document
that includes information about the tables and fields in your database.
The program documents: Primary Keys, Field Information (type, size,
defaults, nullable), Indexes, Check Constraints, Descriptions/Comments,
Foreign Keys, and Triggers. The program also lets you annotate your
tables and fields, and include those comments in the Word doc.
Looking for SQL-Server documenter
I'm looking for a documenter for the SQL-Server. It should describe the
tables, views with its structure (fieldnames, type, length, etc.) and
equally important it should be possible to export this information in a file
(XML, CSV, whatever).
Does anybody knows a tool like that and if so, where to get it?
Thx in advance
MichaelTry SQl Scribe Docuemtnation Builder
http://www.ag-software.com
--
kind regards
Greg O
Need to document your databases. Use the first and still the best AGS SQL
Scribe
http://www.ag-software.com
"Michael Bender" <technik@.salescom.de> wrote in message
news:df69m7$kjp$03$1@.news.t-online.com...
> Hi everybody,
> I'm looking for a documenter for the SQL-Server. It should describe the
> tables, views with its structure (fieldnames, type, length, etc.) and
> equally important it should be possible to export this information in a
> file
> (XML, CSV, whatever).
> Does anybody knows a tool like that and if so, where to get it?
> Thx in advance
> Michael
>|||Michael Bender wrote:
> Hi everybody,
> I'm looking for a documenter for the SQL-Server. It should describe the
> tables, views with its structure (fieldnames, type, length, etc.) and
> equally important it should be possible to export this information in a fi
le
> (XML, CSV, whatever).
> Does anybody knows a tool like that and if so, where to get it?
> Thx in advance
> Michael
SchemaToDoc (http://www.schematodoc.com) will produce a Word document
that includes information about the tables and fields in your database.
The program documents: Primary Keys, Field Information (type, size,
defaults, nullable), Indexes, Check Constraints, Descriptions/Comments,
Foreign Keys, and Triggers. The program also lets you annotate your
tables and fields, and include those comments in the Word doc.
Saturday, February 25, 2012
Looking for query equivalent
To extract a certain type of information we use a query like this in Oracle:
WHERE MOD(TRUNC(ACCOUNT_TYPE / POWER(2,0)),1) > 0
I am hoping someone can help me with the equivalent query syntax for sql server
Scott
Does POWER(2,0) mean "two to the zero power"? If so, why not just write 1? It appears that POWER(2,x) mean the same in both dialects.
Does MOD(x,1) mean MODULUS? Are you wanting the fractional part of x? If so try x - floor(x)
I am guessing that floor(x) is the same as TRUNC(x)
The "Modulus" operator in SQL server is the "%" operator; for instance 17%3 = 2; 21%10=1
Give a look to "Mathematical Functions" in books online
(Somebody check me, please)
|||Do would it look something like this:
MOD(m,n) - where mod returns the remainder of m divided by n
TRUNC(num, x) - A number is truncated to x decimal places
Power(x,y) - x raised to the y power
Oracle Version: MOD(TRUNC(ACCOUNT_TYPE / POWER(2, $x)), 2) > 0
SQL Version: floor(ACCOUNT_TYPE / POWER(2, $x))% 2) > 0
Maybe?
|||If your looking to deterine the xth bit, probably:
set nocount on
declare @.mockUp table
( ACCOUNT_TYPE integer,
powerOfTwo tinyint
)
insert into @.mockUp values (29, 3)
insert into @.mockUp values (14, 0)
insert into @.mockUp values (3, 2)
insert into @.mockup values (900, 7)
insert into @.mockup values (625, 5)
insert into @.mockup values (convert(int, 0xffffffff), 1)
insert into @.mockup values (convert(int, 0xffffffff), 2)
insert into @.mockup values (convert(int, 0xffffffff), 3)
insert into @.mockup values (convert(int, 0xffffffff), 4)
insert into @.mockup values (convert(int, 0xffffffff), 5)
insert into @.mockup values (convert(int, 0xffffffff), 6)
insert into @.mockup values (convert(int, 0xffffffff), 7)
insert into @.mockup values (convert(int, 0xffffffff), 8)
insert into @.mockup values (convert(int, 0xffffffff), 9)
insert into @.mockup values (convert(int, 0xffffffff), 10)
insert into @.mockup values (convert(int, 0xffffffff), 11)
insert into @.mockup values (convert(int, 0xffffffff), 12)
insert into @.mockup values (convert(int, 0xffffffff), 13)
insert into @.mockup values (convert(int, 0xffffffff), 14)
insert into @.mockup values (convert(int, 0xffffffff), 15)
insert into @.mockup values (convert(int, 0xffffffff), 16)
insert into @.mockup values (convert(int, 0xffffffff), 17)
insert into @.mockup values (convert(int, 0xffffffff), 18)
insert into @.mockup values (convert(int, 0xffffffff), 19)
insert into @.mockup values (convert(int, 0xffffffff), 20)
insert into @.mockup values (convert(int, 0xffffffff), 21)
insert into @.mockup values (convert(int, 0xffffffff), 22)
insert into @.mockup values (convert(int, 0xffffffff), 23)
insert into @.mockup values (convert(int, 0xffffffff), 24)
insert into @.mockup values (convert(int, 0xffffffff), 25)
insert into @.mockup values (convert(int, 0xffffffff), 26)
insert into @.mockup values (convert(int, 0xffffffff), 27)
insert into @.mockup values (convert(int, 0xffffffff), 28)
insert into @.mockup values (convert(int, 0xffffffff), 29)
insert into @.mockup values (convert(int, 0xffffffff), 30)select ACCOUNT_TYPE,
powerOfTwo,
floor(ACCOUNT_TYPE / POWER(2, powerOfTwo))% 2 as [floor routine],
(ACCOUNT_TYPE & POWER(2, powerOfTwo))/power(2, powerOfTwo) as [Masked routine]
from @.mockUp-- ACCOUNT_TYPE powerOfTwo floor routine Masked routine
-- - - --
-- 29 3 1 1
-- 14 0 0 0
-- 3 2 0 0
-- 900 7 1 1
-- 625 5 1 1
-- -1 1 0 1
-- -1 2 0 1
-- ...
-- -1 29 0 1
-- -1 30 0 1
Monday, February 20, 2012
Looking for help
I am looking for some type of example where we have a mailbox called
"sqlserver" When a user sends an email to that box with a specific title we
would like to do specific actions. For example is a user sends an xml
attachment with the subject line "sales report" we would like the data to be
imported into our sql2k box. How can we have sql monitor the email box, read
the emails, and process based upon the subject line. Any examples or
suggestions would be greatly appreciated.
Jake"Jake" <rondican@.hotmail.com> wrote in message
news:eqohc7KqEHA.1688@.TK2MSFTNGP10.phx.gbl...
> Hello,
> I am looking for some type of example where we have a mailbox called
> "sqlserver" When a user sends an email to that box with a specific title
we
> would like to do specific actions. For example is a user sends an xml
> attachment with the subject line "sales report" we would like the data to
be
> imported into our sql2k box. How can we have sql monitor the email box,
read
> the emails, and process based upon the subject line. Any examples or
> suggestions would be greatly appreciated.
Setup a scheduled task using task manager to run say every 5 minutes and
have it execute xp_readmail.
However, I'd highly recommend you give serious thought to your security if
you do this.
> Jake
>
Looking for help
I am looking for some type of example where we have a mailbox called
"sqlserver" When a user sends an email to that box with a specific title we
would like to do specific actions. For example is a user sends an xml
attachment with the subject line "sales report" we would like the data to be
imported into our sql2k box. How can we have sql monitor the email box, read
the emails, and process based upon the subject line. Any examples or
suggestions would be greatly appreciated.
Jake
"Jake" <rondican@.hotmail.com> wrote in message
news:eqohc7KqEHA.1688@.TK2MSFTNGP10.phx.gbl...
> Hello,
> I am looking for some type of example where we have a mailbox called
> "sqlserver" When a user sends an email to that box with a specific title
we
> would like to do specific actions. For example is a user sends an xml
> attachment with the subject line "sales report" we would like the data to
be
> imported into our sql2k box. How can we have sql monitor the email box,
read
> the emails, and process based upon the subject line. Any examples or
> suggestions would be greatly appreciated.
Setup a scheduled task using task manager to run say every 5 minutes and
have it execute xp_readmail.
However, I'd highly recommend you give serious thought to your security if
you do this.
> Jake
>