Showing posts with label rows. Show all posts
Showing posts with label rows. Show all posts

Monday, March 26, 2012

How to create hash function for NTEXT

I have a table
create table t
(
val ntext(16),
id int not null identity,
hash nvarchar(32)
)
I'd like to avoid rows with double ntext values, thats why I want to compare
hast values only.
But how can I create hash for ntext ?
ThanksOleg,
You could try using checksum_agg after chunking the ntext column
into 8000 byte pieces. Here's an example using image (there is a long
image column in a Northwind table that makes it easier to write a repro).
checksum() and checksum_agg() are not good hash functions, but they
will help you identify rows that may be the same. You will still have to
check to see if they are the same, as you would with any hash function.
create table Integers (
chunk int primary key
)
insert into Integers select ProductID-1 from Northwind..Products
go
create table textVals (
pk int identity primary key,
t image,
hashVal int
)
insert into textVals select photo, null from Northwind..Employees
go
update textVals set
hashVal = (
select checksum_agg(ca) from (
select checksum(substring(tv.t,1+8000*chunk,8000)) as ca
from Integers, textVals as tv
where tv.pk = textVals.pk and chunk <= datalength(t)/8000
) T
)
select datalength(t) as t_len, hashVal from textVals
go
drop table Integers, textVals
-- Steve Kass
-- Drew University
Oleg Cherkasenko wrote:
>I have a table
>create table t
>(
>val ntext(16),
>id int not null identity,
>hash nvarchar(32)
>)
>I'd like to avoid rows with double ntext values, thats why I want to compare
>hast values only.
>But how can I create hash for ntext ?
>Thanks
>
>

how to CREATE duplicate rows

this may sound like a weird one, but i need to create duplicates of all rows that satisfy a condition.

using asp, i am able to select rows from a databate using a recordset, only to insert it straight back into the database, thus assigning it a new unique id.

but is there any one to perform this action just using sql?

thanks,

goran.insert your_table
select * from your_table x
inner join (
select field_you_want_to_have_duplicates_on, cnt=count(*)
from your_table group by field_you_want_to_have_duplicates_on
having count(*) > 1) y
on x.field_you_want_to_have_duplicates_on = y.field_you_want_to_have_duplicates_on|||I think I just heard their head explode...

They want to do it from asp...that's a pure sql solution (which is the fastest btw)

Just loop through you're rs and do an insert for every iteration...|||i will disagree
if they truly wanted to do it from asp, why did they come to the sql forum??

they want sql and they know it!! and there's no denying

isnt that right rdjabarov?
rd? rd... RD!
well he HAD my back on this..|||AAAAAAAAAAAAAAAAAAAAAAAAAAAARRRRRRRRRRHHHHHHHH!!!

I'm on 2 conf calls and typing here|||hmmm ... i think you guys got me wrong, my current solution is implemented in asp using the same method that me and you have described. i was seeking a pure SQL solution.

anyway, thanks for replying, i've tried adapting your sql statement to my tables but am not getting very far. let me explain a little further ...

i have a table called 'tblWinesEnPrimeur' and want to make a duplicate of every row in this table that has a 'SectionID' of 21.

thanks,
goran.|||Simply remove all your primary keys and constraints, and your users will surely create the duplicate records for you.|||Originally posted by blindman
Simply remove all your primary keys and constraints, and your users will surely create the duplicate records for you.

Very helpful...

I'm betting you have an IDENTITY Column...ket's say Col1

INSERT INTO tblWinesEnPrimeu (Col2, Col3, Col4, ect)
SELECT Col2, Col3, Col4, ect)
FROM tblWinesEnPrimeu
WHERE SectionId = 21

Without the DDL it's a guess...|||I'd suggest using:INSERT INTO yourTable (collist)
SELECT *
FROM yourTable
WHERE 21 = sectionIdTHis should massively confuse your data, and make the users very happy!

-PatP|||sorry guys, no luck with that either, it's on the client's server and we have no direct access to the database, also the website was orignally designed so that duplicates are not inserted by front-end users of the site.

the sql threw up the same exception as before, simple 'Object required'. like i said it's all workin using the asp/sql combined solution, but the client prefers straight forward sql scripts rather than asp pages which can be run many times and inflate or alter the database.|||I give up...I'm totally lost...

Maybe if you post some (any) code, it might help...|||I'll second that thought! I'd suggest that you use SQL Enterprise Mangler to script everything about that table, the DDL, the indicies, even the permissions. Given that and the nickel description that you posted above, I'll bet that one of us can work out the solution!

-PatP|||insert your_table
select * from your_table x
inner join (
select field_you_want_to_have_duplicates_on, cnt=count(*)
from your_table group by field_you_want_to_have_duplicates_on
having count(*) > 1) y
on x.field_you_want_to_have_duplicates_on = y.field_you_want_to_have_duplicates_on
where x.SectionID = 21

Friday, March 23, 2012

How to create computed columns(dynamic) in matrix

Hello,
I am using matrix control for which the rows, columns are all dynamic(
based on the group i specify). I am able to get the Subtotal for both
the rows and colums, which is Great!!..
my problem is i need to add more columns(some formula columns) which
might have "% difference", "varience" etc..
the report should look something like this (M1, M2 are model_id's)
Div/Sec M1 M2 Variance % Difference
----
O3580 0.71 1.47 -0.76 -107.04
8040 1.33 1.33 0 0
8110 9.98 14.47 -4.49 -44.99
11210 5.44 6.36 -0.92 -16.91
and my query is :
select div_sec, model_ID, model_time from table1
Any help is greatly appreciated.
Thanks,
VenkatSeems like this is not possible with reporting services!!...
venkat.oar@.gmail.com wrote:
> Hello,
> I am using matrix control for which the rows, columns are all dynamic(
> based on the group i specify). I am able to get the Subtotal for both
> the rows and colums, which is Great!!..
> my problem is i need to add more columns(some formula columns) which
> might have "% difference", "varience" etc..
> the report should look something like this (M1, M2 are model_id's)
> Div/Sec M1 M2 Variance % Difference
> ----
> O3580 0.71 1.47 -0.76 -107.04
> 8040 1.33 1.33 0 0
> 8110 9.98 14.47 -4.49 -44.99
> 11210 5.44 6.36 -0.92 -16.91
> and my query is :
> select div_sec, model_ID, model_time from table1
> Any help is greatly appreciated.
> Thanks,
> Venkat|||Yes when you select datasets from view menu and right click on the datasets
window you can see a add option. click and you can add calculated fields.
Amarnath
"venkat.oar@.gmail.com" wrote:
> Seems like this is not possible with reporting services!!...
> venkat.oar@.gmail.com wrote:
> > Hello,
> >
> > I am using matrix control for which the rows, columns are all dynamic(
> > based on the group i specify). I am able to get the Subtotal for both
> > the rows and colums, which is Great!!..
> >
> > my problem is i need to add more columns(some formula columns) which
> > might have "% difference", "varience" etc..
> >
> > the report should look something like this (M1, M2 are model_id's)
> >
> > Div/Sec M1 M2 Variance % Difference
> > ----
> > O3580 0.71 1.47 -0.76 -107.04
> > 8040 1.33 1.33 0 0
> > 8110 9.98 14.47 -4.49 -44.99
> > 11210 5.44 6.36 -0.92 -16.91
> >
> > and my query is :
> >
> > select div_sec, model_ID, model_time from table1
> >
> > Any help is greatly appreciated.
> >
> > Thanks,
> > Venkat
>|||Sorry.. i think u got me wrong here. I am able to add calculated fields
but the data what i have is part of 2 rows.
here's a brief description for you.
I am using matrix control for which the rows, columns are all dynamic(
based on the group i specify). I am able to get the Subtotal for both
the rows and colums, which is Great!!..
my problem is i need to add more columns(some formula columns) which
might have "% difference", "varience" etc..
the report should look something like this (M1, M2 are model_id's)
Div/Sec M1 M2 Variance % Difference
----==AD--
O3580 0.71 1.47 -0.76 -107.04
8040 1.33 1.33 0 0
8110 9.98 14.47 -4.49 -44.99
11210 5.44 6.36 -0.92 -16.91
and my query is :
** *** READ BELOW QUERY..
select div_sec, model_ID, model_time from table1
the output of my query is below( seperated by commas)
div_sec, model_ID, model_time
O3580,M1,0.7
O3580,M2,1.47
8040 ,M1,1.33
8040 ,M2,1.33
8110 ,M1,9.98
8110 ,M2,14.47
11210 ,M1,5.44
11210 ,M2,6.36
Any help is greatly appreciated.
Thanks,
Venkat
Amarnath wrote:
> Yes when you select datasets from view menu and right click on the datase=ts
> window you can see a add option. click and you can add calculated fields.
> Amarnath
> "venkat.oar@.gmail.com" wrote:
> > Seems like this is not possible with reporting services!!...
> >
> > venkat.oar@.gmail.com wrote:
> > > Hello,
> > >
> > > I am using matrix control for which the rows, columns are all dynamic(
> > > based on the group i specify). I am able to get the Subtotal for both
> > > the rows and colums, which is Great!!..
> > >
> > > my problem is i need to add more columns(some formula columns) which
> > > might have "% difference", "varience" etc..
> > >
> > > the report should look something like this (M1, M2 are model_id's)
> > >
> > > Div/Sec M1 M2 Variance % Difference
> > > ---=--
> > > O3580 0.71 1.47 -0.76 -107.04
> > > 8040 1.33 1.33 0 0
> > > 8110 9.98 14.47 -4.49 -44.99
> > > 11210 5.44 6.36 -0.92 -16.91
> > >
> > > and my query is :
> > >
> > > select div_sec, model_ID, model_time from table1
> > >
> > > Any help is greatly appreciated.
> > > > > > Thanks,
> > > Venkat
> > > >|||Is there a way in this scenario to use the ReportItems!<textbox>.Value
syntax? Or, does it fall apart because it's a matrix?
venkat.oar@.gmail.com wrote:
> Sorry.. i think u got me wrong here. I am able to add calculated fields
> but the data what i have is part of 2 rows.
> here's a brief description for you.
>
> I am using matrix control for which the rows, columns are all dynamic(
> based on the group i specify). I am able to get the Subtotal for both
> the rows and colums, which is Great!!..
>
> my problem is i need to add more columns(some formula columns) which
> might have "% difference", "varience" etc..
>
> the report should look something like this (M1, M2 are model_id's)
>
> Div/Sec M1 M2 Variance % Difference
> ----­--
> O3580 0.71 1.47 -0.76 -107.04
> 8040 1.33 1.33 0 0
> 8110 9.98 14.47 -4.49 -44.99
> 11210 5.44 6.36 -0.92 -16.91
>
> and my query is :
> ** *** READ BELOW QUERY..
> select div_sec, model_ID, model_time from table1
> the output of my query is below( seperated by commas)
> div_sec, model_ID, model_time
> O3580,M1,0.7
> O3580,M2,1.47
> 8040 ,M1,1.33
> 8040 ,M2,1.33
> 8110 ,M1,9.98
> 8110 ,M2,14.47
> 11210 ,M1,5.44
> 11210 ,M2,6.36
> Any help is greatly appreciated.
>
> Thanks,
> Venkat
> Amarnath wrote:
>>Yes when you select datasets from view menu and right click on the datasets
>>window you can see a add option. click and you can add calculated fields.
>>Amarnath
>>"venkat.oar@.gmail.com" wrote:
>>
>>Seems like this is not possible with reporting services!!...
>>venkat.oar@.gmail.com wrote:
>>Hello,
>>I am using matrix control for which the rows, columns are all dynamic(
>>based on the group i specify). I am able to get the Subtotal for both
>>the rows and colums, which is Great!!..
>>my problem is i need to add more columns(some formula columns) which
>>might have "% difference", "varience" etc..
>>the report should look something like this (M1, M2 are model_id's)
>>Div/Sec M1 M2 Variance % Difference
>>----
>>O3580 0.71 1.47 -0.76 -107.04
>>8040 1.33 1.33 0 0
>>8110 9.98 14.47 -4.49 -44.99
>>11210 5.44 6.36 -0.92 -16.91
>>and my query is :
>>select div_sec, model_ID, model_time from table1
>>Any help is greatly appreciated.
>>Thanks,
>>Venkat
>>
>|||Mike,
I can't even go that far to use ReportItems!<txtbox>.value, the reason
is when i add column to the matrix, the columns appear for each column
it is created dynamically( in my case the columns appear for each
model_ID).
Still no clue how to do this or is it anyway possible?.
Michael Cervantes wrote:
> Is there a way in this scenario to use the ReportItems!<textbox>.Value
> syntax? Or, does it fall apart because it's a matrix?
>
> venkat.oar@.gmail.com wrote:
> > Sorry.. i think u got me wrong here. I am able to add calculated fields
> > but the data what i have is part of 2 rows.
> > here's a brief description for you.
> >
> >
> > I am using matrix control for which the rows, columns are all dynamic(
> > based on the group i specify). I am able to get the Subtotal for both
> > the rows and colums, which is Great!!..
> >
> >
> > my problem is i need to add more columns(some formula columns) which
> > might have "% difference", "varience" etc..
> >
> >
> > the report should look something like this (M1, M2 are model_id's)
> >
> >
> > Div/Sec M1 M2 Variance % Difference
> > ----=--=AD--
> >
> > O3580 0.71 1.47 -0.76 -107.04
> > 8040 1.33 1.33 0 0
> > 8110 9.98 14.47 -4.49 -44.99
> > 11210 5.44 6.36 -0.92 -16.91
> >
> >
> > and my query is :
> >
> > ** *** READ BELOW QUERY..
> > select div_sec, model_ID, model_time from table1
> > the output of my query is below( seperated by commas)
> > div_sec, model_ID, model_time
> > O3580,M1,0.7
> > O3580,M2,1.47
> > 8040 ,M1,1.33
> > 8040 ,M2,1.33
> > 8110 ,M1,9.98
> > 8110 ,M2,14.47
> > 11210 ,M1,5.44
> > 11210 ,M2,6.36
> >
> > Any help is greatly appreciated.
> >
> >
> > Thanks,
> > Venkat
> >
> > Amarnath wrote:
> >
> >>Yes when you select datasets from view menu and right click on the data=sets
> >>window you can see a add option. click and you can add calculated field=s=2E
> >>
> >>Amarnath
> >>
> >>"venkat.oar@.gmail.com" wrote:
> >>
> >>
> >>Seems like this is not possible with reporting services!!...
> >>
> >>venkat.oar@.gmail.com wrote:
> >>
> >>Hello,
> >>
> >>I am using matrix control for which the rows, columns are all dynamic(
> >>based on the group i specify). I am able to get the Subtotal for both
> >>the rows and colums, which is Great!!..
> >>
> >>my problem is i need to add more columns(some formula columns) which
> >>might have "% difference", "varience" etc..
> >>
> >>the report should look something like this (M1, M2 are model_id's)
> >>
> >>Div/Sec M1 M2 Variance % Difference
> >>---=--
> >>O3580 0.71 1.47 -0.76 -107.04
> >>8040 1.33 1.33 0 0
> >>8110 9.98 14.47 -4.49 -44.99
> >>11210 5.44 6.36 -0.92 -16.91
> >>
> >>and my query is :
> >>
> >>select div_sec, model_ID, model_time from table1
> >>
> >>Any help is greatly appreciated.
> >>
> >>Thanks,
> >>Venkat
> >>
> >>
> >|||Hi try getting all the required data from query, here is the query for that.
see if it works for you.. I think you can put this in table object in SSRS as
well.
select div_sec,
sum((case (model_ID) when 'M1' then (model_time) end)) M1,
sum((case (model_ID) when 'M2' then (model_time) end)) M2,
sum((case (model_ID) when 'M1' then (model_time) end)) - sum((case
(model_ID) when 'M2' then (model_time) end)) Variance
from #temp
group by div_sec
order by 1
You can replace #temp with your table and you can add more columns as well.
This is basically converting all vertical data to horizontal ie pivoting.
Amarnath
"venkat.oar@.gmail.com" wrote:
> Mike,
> I can't even go that far to use ReportItems!<txtbox>.value, the reason
> is when i add column to the matrix, the columns appear for each column
> it is created dynamically( in my case the columns appear for each
> model_ID).
> Still no clue how to do this or is it anyway possible?.
>
> Michael Cervantes wrote:
> > Is there a way in this scenario to use the ReportItems!<textbox>.Value
> > syntax? Or, does it fall apart because it's a matrix?
> >
> >
> >
> > venkat.oar@.gmail.com wrote:
> >
> > > Sorry.. i think u got me wrong here. I am able to add calculated fields
> > > but the data what i have is part of 2 rows.
> > > here's a brief description for you.
> > >
> > >
> > > I am using matrix control for which the rows, columns are all dynamic(
> > > based on the group i specify). I am able to get the Subtotal for both
> > > the rows and colums, which is Great!!..
> > >
> > >
> > > my problem is i need to add more columns(some formula columns) which
> > > might have "% difference", "varience" etc..
> > >
> > >
> > > the report should look something like this (M1, M2 are model_id's)
> > >
> > >
> > > Div/Sec M1 M2 Variance % Difference
> > > ----­--
> > >
> > > O3580 0.71 1.47 -0.76 -107.04
> > > 8040 1.33 1.33 0 0
> > > 8110 9.98 14.47 -4.49 -44.99
> > > 11210 5.44 6.36 -0.92 -16.91
> > >
> > >
> > > and my query is :
> > >
> > > ** *** READ BELOW QUERY..
> > > select div_sec, model_ID, model_time from table1
> > > the output of my query is below( seperated by commas)
> > > div_sec, model_ID, model_time
> > > O3580,M1,0.7
> > > O3580,M2,1.47
> > > 8040 ,M1,1.33
> > > 8040 ,M2,1.33
> > > 8110 ,M1,9.98
> > > 8110 ,M2,14.47
> > > 11210 ,M1,5.44
> > > 11210 ,M2,6.36
> > >
> > > Any help is greatly appreciated.
> > >
> > >
> > > Thanks,
> > > Venkat
> > >
> > > Amarnath wrote:
> > >
> > >>Yes when you select datasets from view menu and right click on the datasets
> > >>window you can see a add option. click and you can add calculated fields.
> > >>
> > >>Amarnath
> > >>
> > >>"venkat.oar@.gmail.com" wrote:
> > >>
> > >>
> > >>Seems like this is not possible with reporting services!!...
> > >>
> > >>venkat.oar@.gmail.com wrote:
> > >>
> > >>Hello,
> > >>
> > >>I am using matrix control for which the rows, columns are all dynamic(
> > >>based on the group i specify). I am able to get the Subtotal for both
> > >>the rows and colums, which is Great!!..
> > >>
> > >>my problem is i need to add more columns(some formula columns) which
> > >>might have "% difference", "varience" etc..
> > >>
> > >>the report should look something like this (M1, M2 are model_id's)
> > >>
> > >>Div/Sec M1 M2 Variance % Difference
> > >>----
> > >>O3580 0.71 1.47 -0.76 -107.04
> > >>8040 1.33 1.33 0 0
> > >>8110 9.98 14.47 -4.49 -44.99
> > >>11210 5.44 6.36 -0.92 -16.91
> > >>
> > >>and my query is :
> > >>
> > >>select div_sec, model_ID, model_time from table1
> > >>
> > >>Any help is greatly appreciated.
> > >>
> > >>Thanks,
> > >>Venkat
> > >>
> > >>
> > >
>|||Hi Venkat, my problem (as posted on 5th July, "Calculations in Matrix
report") is almost identical, so I'll let you know if a solution/workaround
can be found.
Pete
<venkat.oar@.gmail.com> wrote in message
news:1152882808.590984.275540@.i42g2000cwa.googlegroups.com...
Mike,
I can't even go that far to use ReportItems!<txtbox>.value, the reason
is when i add column to the matrix, the columns appear for each column
it is created dynamically( in my case the columns appear for each
model_ID).
Still no clue how to do this or is it anyway possible?.
Michael Cervantes wrote:
> Is there a way in this scenario to use the ReportItems!<textbox>.Value
> syntax? Or, does it fall apart because it's a matrix?
>
> venkat.oar@.gmail.com wrote:
> > Sorry.. i think u got me wrong here. I am able to add calculated fields
> > but the data what i have is part of 2 rows.
> > here's a brief description for you.
> >
> >
> > I am using matrix control for which the rows, columns are all dynamic(
> > based on the group i specify). I am able to get the Subtotal for both
> > the rows and colums, which is Great!!..
> >
> >
> > my problem is i need to add more columns(some formula columns) which
> > might have "% difference", "varience" etc..
> >
> >
> > the report should look something like this (M1, M2 are model_id's)
> >
> >
> > Div/Sec M1 M2 Variance % Difference
> > ----­--
> >
> > O3580 0.71 1.47 -0.76 -107.04
> > 8040 1.33 1.33 0 0
> > 8110 9.98 14.47 -4.49 -44.99
> > 11210 5.44 6.36 -0.92 -16.91
> >
> >
> > and my query is :
> >
> > ** *** READ BELOW QUERY..
> > select div_sec, model_ID, model_time from table1
> > the output of my query is below( seperated by commas)
> > div_sec, model_ID, model_time
> > O3580,M1,0.7
> > O3580,M2,1.47
> > 8040 ,M1,1.33
> > 8040 ,M2,1.33
> > 8110 ,M1,9.98
> > 8110 ,M2,14.47
> > 11210 ,M1,5.44
> > 11210 ,M2,6.36
> >
> > Any help is greatly appreciated.
> >
> >
> > Thanks,
> > Venkat
> >
> > Amarnath wrote:
> >
> >>Yes when you select datasets from view menu and right click on the
> >>datasets
> >>window you can see a add option. click and you can add calculated
> >>fields.
> >>
> >>Amarnath
> >>
> >>"venkat.oar@.gmail.com" wrote:
> >>
> >>
> >>Seems like this is not possible with reporting services!!...
> >>
> >>venkat.oar@.gmail.com wrote:
> >>
> >>Hello,
> >>
> >>I am using matrix control for which the rows, columns are all dynamic(
> >>based on the group i specify). I am able to get the Subtotal for both
> >>the rows and colums, which is Great!!..
> >>
> >>my problem is i need to add more columns(some formula columns) which
> >>might have "% difference", "varience" etc..
> >>
> >>the report should look something like this (M1, M2 are model_id's)
> >>
> >>Div/Sec M1 M2 Variance % Difference
> >>----
> >>O3580 0.71 1.47 -0.76 -107.04
> >>8040 1.33 1.33 0 0
> >>8110 9.98 14.47 -4.49 -44.99
> >>11210 5.44 6.36 -0.92 -16.91
> >>
> >>and my query is :
> >>
> >>select div_sec, model_ID, model_time from table1
> >>
> >>Any help is greatly appreciated.
> >>
> >>Thanks,
> >>Venkat
> >>
> >>
> >

How to create and fill a random varbinary table?

Hi all,

For a test I'm performing I need to create a table with varbinary(120000) column.
I need to fill this table with 1000000 rows of random varbinary values. (The values should be from 0x1 till 0xfff....f)
Does anyone have a "nice" way to create this table?


Thanks in advance,
Shai.

You might be able to leverage (1) a table of numbers and (2) a scalar UDF that allows each row to have a different random results. Give a look to this post:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1330539&SiteID=1

|||

You can do something like this:


set nocount on
go
alter function getHexCharacter
(
@.i int
)

--used to return a character 0-9 and A-F for inputs from 0-15
returns char(1)
as
begin
return char(case when @.i <= 9 then @.i + 48
when @.i > 9 and @.i <= 15 then @.i + 55
else null end)
end
go


create table #randomBinaryValue
(
value varbinary(2) constraint PKrandomBinaryValue primary key
)
go
declare @.i int, @.newValue char(4) set @.i = 0
while (1=1)
begin
--build up the text of the
select @.newValue =
'0x' + dbo.getHexCharacter(cast(rand() * 15 as int))
+ dbo.getHexCharacter(cast(rand() * 15 as int))

--add more getHexCharacters to get a longer result
-- + dbo.getHexCharacter(cast(rand() * 15 as int))

begin try
exec (' insert into #randomBinaryValue select ' + @.newValue)
set @.i = @.i + 1
end try
begin catch
select 'don''t update the counter',error_message()
end catch


if @.i > 100 break
end


select *
from #randomBinaryValue
go
drop table #randomBinaryValue

|||

If you really want to fill the random numbers between the given range the following query is more enough..

Code Snippet

CreateTable #randomBinaryValue

(

valuevarbinary(32)constraint PKrandomBinaryValueprimary key nonclustered --Just for verfication/avoid duplicate

);

Go

SETNOCOUNTON;

Declare @.Iasint;

Set @.I= 0;

WHILE @.I< 10000

Begin

InsertINto #randomBinaryValuevalues(convert(varbinary,newid()));

Set @.I= @.I+ 1;

End

Select*From #randomBinaryValue

Go

DropTable #randomBinaryValue

|||

All the ideas are nice but...

casting newid will only generate me a 16 byte varbinary and I need 120000 byte varbinary.

create a 120000 varbinary with a loop and then inserting it into a table will take a long time for 1000000 rows...

Does any one have a quicker way?

Thanks,

Shai.

|||

Shai:

I used this query as the basis to generate the script that follows:

Code Snippet

select ' convert(binary(16), newid()) + '
from small_iterator (nolock)
where iter <= 500

What I found when I ran my mockup is that on my desktop server it takes about 67 ms to create each varbinary(max) record with a length of 120000. Therefore, just to generate 1000000 rows of such on my desktop unit might take on the order of 18 hours! The problem is that creating very long VARBINARY(MAX) data takes a fair amount of time. I used a query like this:

Code Snippet

declare @.begDt datetime set @.begDt = getdate()

declare @.loopLimit integer set @.loopLimit = 10


declare @.loopCt integer
declare @.hugeVarbinary varbinary (max)

set @.loopCt = 1
while @.loopCt <= @.loopLimit
begin

select @.hugeVarbinary =
( select convert(binary(16), newid()) +
convert(binary(16), newid()) +
...

convert(binary(16), newid()) +
convert(binary(16), newid())
)

select @.hugeVarbinary = @.hugeVarbinary +
( select convert(binary(16), newid()) +
...

convert(binary(16), newid()) +
convert(binary(16), newid())
)
from small_iterator (nolock)
where iter <= 14

set @.loopCt = @.loopCt + 1

end

print ' '
select @.loopLimit as [@.loopLimit],
datediff (ms, @.begDt, getdate()) as [Elapsed Time]

/* Times in seconds
10 Records: 0.690 0.686 0.673 Average: 0.686
100 Records: 6.966 6.766 6.890 Average: 6.874
1000 Records: 68.030 67.156 67.436 Average: 67.541
10000 Records: 681.720
*/


|||

Honestly, if you really only just need a random varbinary value, why not just build each new one on the fly? Somthing like this:

create view randomVarbinary as

select

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) as crazyBigVarbinary

go

select *

from randomVarbinary

How to create and fill a random varbinary table?

Hi all,

For a test I'm performing I need to create a table with varbinary(120000) column.
I need to fill this table with 1000000 rows of random varbinary values. (The values should be from 0x1 till 0xfff....f)
Does anyone have a "nice" way to create this table?


Thanks in advance,
Shai.

You might be able to leverage (1) a table of numbers and (2) a scalar UDF that allows each row to have a different random results. Give a look to this post:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1330539&SiteID=1

|||

You can do something like this:


set nocount on
go
alter function getHexCharacter
(
@.i int
)

--used to return a character 0-9 and A-F for inputs from 0-15
returns char(1)
as
begin
return char(case when @.i <= 9 then @.i + 48
when @.i > 9 and @.i <= 15 then @.i + 55
else null end)
end
go


create table #randomBinaryValue
(
value varbinary(2) constraint PKrandomBinaryValue primary key
)
go
declare @.i int, @.newValue char(4) set @.i = 0
while (1=1)
begin
--build up the text of the
select @.newValue =
'0x' + dbo.getHexCharacter(cast(rand() * 15 as int))
+ dbo.getHexCharacter(cast(rand() * 15 as int))

--add more getHexCharacters to get a longer result
-- + dbo.getHexCharacter(cast(rand() * 15 as int))

begin try
exec (' insert into #randomBinaryValue select ' + @.newValue)
set @.i = @.i + 1
end try
begin catch
select 'don''t update the counter',error_message()
end catch


if @.i > 100 break
end


select *
from #randomBinaryValue
go
drop table #randomBinaryValue

|||

If you really want to fill the random numbers between the given range the following query is more enough..

Code Snippet

Create Table #randomBinaryValue

(

valuevarbinary(32) constraint PKrandomBinaryValue primary key nonclustered --Just for verfication/avoid duplicate

);

Go

SET NOCOUNT ON;

Declare @.I as int;

Set @.I = 0;

WHILE @.I < 10000

Begin

Insert INto #randomBinaryValue values(convert(varbinary,newid()));

Set @.I = @.I+ 1;

End

Select * From #randomBinaryValue

Go

Drop Table #randomBinaryValue

|||

All the ideas are nice but...

casting newid will only generate me a 16 byte varbinary and I need 120000 byte varbinary.

create a 120000 varbinary with a loop and then inserting it into a table will take a long time for 1000000 rows...

Does any one have a quicker way?

Thanks,

Shai.

|||

Shai:

I used this query as the basis to generate the script that follows:

Code Snippet

select ' convert(binary(16), newid()) + '
from small_iterator (nolock)
where iter <= 500

What I found when I ran my mockup is that on my desktop server it takes about 67 ms to create each varbinary(max) record with a length of 120000. Therefore, just to generate 1000000 rows of such on my desktop unit might take on the order of 18 hours! The problem is that creating very long VARBINARY(MAX) data takes a fair amount of time. I used a query like this:

Code Snippet

declare @.begDt datetime set @.begDt = getdate()

declare @.loopLimit integer set @.loopLimit = 10


declare @.loopCt integer
declare @.hugeVarbinary varbinary (max)

set @.loopCt = 1
while @.loopCt <= @.loopLimit
begin

select @.hugeVarbinary =
( select convert(binary(16), newid()) +
convert(binary(16), newid()) +
...

convert(binary(16), newid()) +
convert(binary(16), newid())
)

select @.hugeVarbinary = @.hugeVarbinary +
( select convert(binary(16), newid()) +
...

convert(binary(16), newid()) +
convert(binary(16), newid())
)
from small_iterator (nolock)
where iter <= 14

set @.loopCt = @.loopCt + 1

end

print ' '
select @.loopLimit as [@.loopLimit],
datediff (ms, @.begDt, getdate()) as [Elapsed Time]

/* Times in seconds
10 Records: 0.690 0.686 0.673 Average: 0.686
100 Records: 6.966 6.766 6.890 Average: 6.874
1000 Records: 68.030 67.156 67.436 Average: 67.541
10000 Records: 681.720
*/


|||

Honestly, if you really only just need a random varbinary value, why not just build each new one on the fly? Somthing like this:

create view randomVarbinary as

select

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) +

cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) + cast(newId() as varbinary(16)) as crazyBigVarbinary

go

select *

from randomVarbinary

Wednesday, March 21, 2012

How to create a trigger such that it can delete the rows whenever any other application such as

I had created a trigger which sees that whether a database is updated if it is its copy the values of the updated row into another control table now I want to read the content of control_table into BIzTalk and after reading I want to delete it.Can any one suggest the suitable ay to do this?

Hi,

actually Select has no trigger action, only DML operations are capable. You could use a stored procedure to read the rows rather than using a select statement (don′t know if BizTalk is able to do this through stored procedures).

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Thanks a lot Jens.

How to create a trigger such that it can delete the rows whenever any other application such as

I had created a trigger which sees that whether a database is updated if it is its copy the values of the updated row into another control table now I want to read the content of control_table into BIzTalk and after reading I want to delete it.Can any one suggest the suitable ay to do this?

Hi,

actually Select has no trigger action, only DML operations are capable. You could use a stored procedure to read the rows rather than using a select statement (don′t know if BizTalk is able to do this through stored procedures).

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

Monday, March 19, 2012

How to Create a Sequence Generator number in SSIS

Hi,

I got 5000 rows in source and when i am sending the data to destination it has to create a sequence generator number for each row.

Can any one help me which transformation do i need to take for doing this in SSIS.

Row Number Transformation
(http://www.sqlis.com/default.aspx?93)|||

Alternatively you can also look the following articles

Surrogate key generation in SSIS @. http://www.sqljunkies.com/WebLog/sqlbi/archive/2005/05/30/15684.aspx

Generating Surrogate Keys @. http://www.sqlis.com/default.aspx?37

Thanks,
Loonysan
http://mystutter.blogspot.com/

Wednesday, March 7, 2012

How to count up group number

Hi,
I have a report with a group and detail rows. I want to count up the groups
in the column groupnumber.
I tried the rownumber(<scope>) function.
If I use rownumber (Nothing) the detail rows were counted.
If I use rownumber("Groupname") the Report Server shows the number of detail
rows for each group.
Which scope should I use to count up the single groups?
Best regards,
StefanMaybe declare a global variable (i) in the Custom code and add this variable
in the value of a field in your group (i.e. =i+1)
"Stefoon23" wrote:
> Hi,
> I have a report with a group and detail rows. I want to count up the groups
> in the column groupnumber.
> I tried the rownumber(<scope>) function.
> If I use rownumber (Nothing) the detail rows were counted.
> If I use rownumber("Groupname") the Report Server shows the number of detail
> rows for each group.
> Which scope should I use to count up the single groups?
> Best regards,
> Stefan|||Hi Kyriakos,
your answer makes sense. But I couldn't get it to work!
How do I declare a custom global variable and how could I access it in the
report?
Best regards,
Stefan
"Kyriakos" wrote:
> Maybe declare a global variable (i) in the Custom code and add this variable
> in the value of a field in your group (i.e. =i+1)
> "Stefoon23" wrote:
> > Hi,
> >
> > I have a report with a group and detail rows. I want to count up the groups
> > in the column groupnumber.
> >
> > I tried the rownumber(<scope>) function.
> > If I use rownumber (Nothing) the detail rows were counted.
> > If I use rownumber("Groupname") the Report Server shows the number of detail
> > rows for each group.
> > Which scope should I use to count up the single groups?
> >
> > Best regards,
> > Stefan|||Add this code at your custom code section
============Public Shared i as integer
Public Function GetGrp() As Integer
i +=1
Return i
End Function
============
then call the function within a field on the group level, i.e. =Code.GetGrp()
That should do the count.
"Stefoon23" wrote:
> Hi Kyriakos,
> your answer makes sense. But I couldn't get it to work!
> How do I declare a custom global variable and how could I access it in the
> report?
> Best regards,
> Stefan
> "Kyriakos" wrote:
> > Maybe declare a global variable (i) in the Custom code and add this variable
> > in the value of a field in your group (i.e. =i+1)
> >
> > "Stefoon23" wrote:
> >
> > > Hi,
> > >
> > > I have a report with a group and detail rows. I want to count up the groups
> > > in the column groupnumber.
> > >
> > > I tried the rownumber(<scope>) function.
> > > If I use rownumber (Nothing) the detail rows were counted.
> > > If I use rownumber("Groupname") the Report Server shows the number of detail
> > > rows for each group.
> > > Which scope should I use to count up the single groups?
> > >
> > > Best regards,
> > > Stefan|||Hi Kyriakos, it worked perfectly!
Thanks for your help!
Stefoon
"Kyriakos" wrote:
> Add this code at your custom code section
> ============> Public Shared i as integer
> Public Function GetGrp() As Integer
> i +=1
> Return i
> End Function
> ============> then call the function within a field on the group level, i.e. =Code.GetGrp()
>
> That should do the count.
> "Stefoon23" wrote:
> > Hi Kyriakos,
> > your answer makes sense. But I couldn't get it to work!
> > How do I declare a custom global variable and how could I access it in the
> > report?
> >
> > Best regards,
> > Stefan
> >
> > "Kyriakos" wrote:
> >
> > > Maybe declare a global variable (i) in the Custom code and add this variable
> > > in the value of a field in your group (i.e. =i+1)
> > >
> > > "Stefoon23" wrote:
> > >
> > > > Hi,
> > > >
> > > > I have a report with a group and detail rows. I want to count up the groups
> > > > in the column groupnumber.
> > > >
> > > > I tried the rownumber(<scope>) function.
> > > > If I use rownumber (Nothing) the detail rows were counted.
> > > > If I use rownumber("Groupname") the Report Server shows the number of detail
> > > > rows for each group.
> > > > Which scope should I use to count up the single groups?
> > > >
> > > > Best regards,
> > > > Stefan|||Hi Kyriakos,
I wrote it works perfectly. It works when I Render the report the first
time. But when I choose other parameters the counting is not initialized.
Thus it does not start at zero but at the highest number.
e.g. I have a report with 5 group members for a group level the count would
go from 1 to 5 the first time I render the report. The second time the
numbers would reach from 6 to 10. ;-)
How could I initialize the value of the global variable every time the
report is rendered again? Or maybe it would be a solution to init the value
when the report has finished rendering.
Best regards,
Stefoon
"Kyriakos" wrote:
> Add this code at your custom code section
> ============> Public Shared i as integer
> Public Function GetGrp() As Integer
> i +=1
> Return i
> End Function
> ============> then call the function within a field on the group level, i.e. =Code.GetGrp()
>
> That should do the count.
> "Stefoon23" wrote:
> > Hi Kyriakos,
> > your answer makes sense. But I couldn't get it to work!
> > How do I declare a custom global variable and how could I access it in the
> > report?
> >
> > Best regards,
> > Stefan
> >
> > "Kyriakos" wrote:
> >
> > > Maybe declare a global variable (i) in the Custom code and add this variable
> > > in the value of a field in your group (i.e. =i+1)
> > >
> > > "Stefoon23" wrote:
> > >
> > > > Hi,
> > > >
> > > > I have a report with a group and detail rows. I want to count up the groups
> > > > in the column groupnumber.
> > > >
> > > > I tried the rownumber(<scope>) function.
> > > > If I use rownumber (Nothing) the detail rows were counted.
> > > > If I use rownumber("Groupname") the Report Server shows the number of detail
> > > > rows for each group.
> > > > Which scope should I use to count up the single groups?
> > > >
> > > > Best regards,
> > > > Stefan|||I could solve the puzzle by myself:
add some more custom code to your project
===================Public Shared i as integer = 0
Public Function GetGrp() As Integer
i +=1
Return i
End Function
Public Function InitGrp() As Integer
i=0
Return i
End Function
=======================
Put the Function InitGrp() to the page header.
Best regards,
Stefoon
"Stefoon23" wrote:
> Hi Kyriakos,
> I wrote it works perfectly. It works when I Render the report the first
> time. But when I choose other parameters the counting is not initialized.
> Thus it does not start at zero but at the highest number.
> e.g. I have a report with 5 group members for a group level the count would
> go from 1 to 5 the first time I render the report. The second time the
> numbers would reach from 6 to 10. ;-)
> How could I initialize the value of the global variable every time the
> report is rendered again? Or maybe it would be a solution to init the value
> when the report has finished rendering.
> Best regards,
> Stefoon
>
> "Kyriakos" wrote:
> > Add this code at your custom code section
> >
> > ============> > Public Shared i as integer
> >
> > Public Function GetGrp() As Integer
> > i +=1
> > Return i
> > End Function
> > ============> >
> > then call the function within a field on the group level, i.e. =Code.GetGrp()
> >
> >
> > That should do the count.
> >
> > "Stefoon23" wrote:
> >
> > > Hi Kyriakos,
> > > your answer makes sense. But I couldn't get it to work!
> > > How do I declare a custom global variable and how could I access it in the
> > > report?
> > >
> > > Best regards,
> > > Stefan
> > >
> > > "Kyriakos" wrote:
> > >
> > > > Maybe declare a global variable (i) in the Custom code and add this variable
> > > > in the value of a field in your group (i.e. =i+1)
> > > >
> > > > "Stefoon23" wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > I have a report with a group and detail rows. I want to count up the groups
> > > > > in the column groupnumber.
> > > > >
> > > > > I tried the rownumber(<scope>) function.
> > > > > If I use rownumber (Nothing) the detail rows were counted.
> > > > > If I use rownumber("Groupname") the Report Server shows the number of detail
> > > > > rows for each group.
> > > > > Which scope should I use to count up the single groups?
> > > > >
> > > > > Best regards,
> > > > > Stefan|||:-)
"Stefoon23" wrote:
> I could solve the puzzle by myself:
> add some more custom code to your project
> ===================> Public Shared i as integer = 0
> Public Function GetGrp() As Integer
> i +=1
> Return i
> End Function
>
> Public Function InitGrp() As Integer
> i=0
> Return i
> End Function
> =======================> Put the Function InitGrp() to the page header.
> Best regards,
> Stefoon
> "Stefoon23" wrote:
> > Hi Kyriakos,
> > I wrote it works perfectly. It works when I Render the report the first
> > time. But when I choose other parameters the counting is not initialized.
> > Thus it does not start at zero but at the highest number.
> > e.g. I have a report with 5 group members for a group level the count would
> > go from 1 to 5 the first time I render the report. The second time the
> > numbers would reach from 6 to 10. ;-)
> >
> > How could I initialize the value of the global variable every time the
> > report is rendered again? Or maybe it would be a solution to init the value
> > when the report has finished rendering.
> >
> > Best regards,
> > Stefoon
> >
> >
> >
> > "Kyriakos" wrote:
> >
> > > Add this code at your custom code section
> > >
> > > ============> > > Public Shared i as integer
> > >
> > > Public Function GetGrp() As Integer
> > > i +=1
> > > Return i
> > > End Function
> > > ============> > >
> > > then call the function within a field on the group level, i.e. =Code.GetGrp()
> > >
> > >
> > > That should do the count.
> > >
> > > "Stefoon23" wrote:
> > >
> > > > Hi Kyriakos,
> > > > your answer makes sense. But I couldn't get it to work!
> > > > How do I declare a custom global variable and how could I access it in the
> > > > report?
> > > >
> > > > Best regards,
> > > > Stefan
> > > >
> > > > "Kyriakos" wrote:
> > > >
> > > > > Maybe declare a global variable (i) in the Custom code and add this variable
> > > > > in the value of a field in your group (i.e. =i+1)
> > > > >
> > > > > "Stefoon23" wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > I have a report with a group and detail rows. I want to count up the groups
> > > > > > in the column groupnumber.
> > > > > >
> > > > > > I tried the rownumber(<scope>) function.
> > > > > > If I use rownumber (Nothing) the detail rows were counted.
> > > > > > If I use rownumber("Groupname") the Report Server shows the number of detail
> > > > > > rows for each group.
> > > > > > Which scope should I use to count up the single groups?
> > > > > >
> > > > > > Best regards,
> > > > > > Stefan

how to count the total rows for a query with group by?

Hi,
I have a query like
select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY from tbl1, tbl2 where
tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
for example, I can get the results like
f1 f2 YY
--
x1 y1 2
x2 y2 5
x3 y3 1
As you can see, total rows for the query is 3 (rows). How can I get
the "3" in the one select query like:
f1 f2 YY Total_Rows
--
x1 y1 2 3
x2 y2 5 3
x3 y3 1 3
or write another query to count the total rows (to get the "3") only?
Thanks a lot.
Ouyanghi
just see this:
select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY ( select count(*) from tbl1,
tbl2 where
tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2 ) as total_rows from tbl1, tbl2
where
tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
probably this solves your problem
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"zxo102" wrote:

> Hi,
> I have a query like
> select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY from tbl1, tbl2 where
> tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
> for example, I can get the results like
>
> f1 f2 YY
> --
> x1 y1 2
> x2 y2 5
> x3 y3 1
>
> As you can see, total rows for the query is 3 (rows). How can I get
> the "3" in the one select query like:
> f1 f2 YY Total_Rows
> --
> x1 y1 2 3
> x2 y2 5 3
> x3 y3 1 3
> or write another query to count the total rows (to get the "3") only?
>
> Thanks a lot.
> Ouyang
>|||You may also want to summarise the data in a report style query using
COMPUTE, the link below will take you to an example on how to implement
this:
http://www.mssql.com.au/kb/html/gmg... />
&@.sa_id=58
"zxo102" <zxo102@.gmail.com> wrote in message
news:1122690598.655507.63480@.g47g2000cwa.googlegroups.com...
> Hi,
> I have a query like
> select tbl1.f1, tbl2.f2, count(tbl1.f1) as YY from tbl1, tbl2 where
> tbl1.f1=tbl2.f2 group by tbl1.f1, tbl2.f2
> for example, I can get the results like
>
> f1 f2 YY
> --
> x1 y1 2
> x2 y2 5
> x3 y3 1
>
> As you can see, total rows for the query is 3 (rows). How can I get
> the "3" in the one select query like:
> f1 f2 YY Total_Rows
> --
> x1 y1 2 3
> x2 y2 5 3
> x3 y3 1 3
> or write another query to count the total rows (to get the "3") only?
>
> Thanks a lot.
> Ouyang
>|||Hi Chandra,
this sql query:
"select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2 group by
tbl1.f1, tbl2.f2"
gives group count instead of total count from the query with "group
by".
Any ideas?
Thanks.
Ouyang|||hi
can you please send the ddl so that i can help u out
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"zxo102" wrote:

> Hi Chandra,
> this sql query:
> "select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2 group by
> tbl1.f1, tbl2.f2"
> gives group count instead of total count from the query with "group
> by".
> Any ideas?
> Thanks.
> Ouyang
>|||hi
i am sorry, you can try this:
select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"zxo102" wrote:

> Hi Chandra,
> this sql query:
> "select count(*) from tbl1, tbl2 where tbl1.f1=tbl2.f2 group by
> tbl1.f1, tbl2.f2"
> gives group count instead of total count from the query with "group
> by".
> Any ideas?
> Thanks.
> Ouyang
>|||Hi Guillaume,
Thanks for your information. I got the total row using "compute".
But the returned results from SQL server are in two parts. The value
of the total row is in the second part.
Since I call this query from python application which can not grab the
value from the second part, do you know how to get the total row in a
query which just return the total row only?
Thanks a lot.
Ouyang|||Hi Chandra,
My ddl is as follows:
select t_all_status.enter_diag AS r_ICDCODE,
count(t_all_status.enter_diag) AS r_SUM2,
t_icd.chinese_diag AS r_CHINESE,
t_dept.dept_name as r_DEPT_NAME
from t_all_status, t_icd,t_dept
where (t_all_status.enter_dept=t_dept.dept_code)
AND (t_all_status.enter_diag not like 'M%')
AND (t_all_status.enter_diag = t_icd.icdcode)
group by t_dept.dept_name,t_all_status.enter_diag, t_icd.chinese_diag
order by r_SUM2 DESC
Thanks
Ouyang|||Put the row count in an output variable. Maybe something like this:
select *
from ...
set @.finalRowCount = @.@.rowcount
@.finalRowCount must be declared as an in/out parameter of your procedure.
ML|||Hi ML,
I don't know very much about the stuff you mentioned, can you give
me an exmaple for that using my DDL as follows:
select t_all_status.enter_diag AS r_ICDCODE,
count(t_all_status.enter_diag) AS r_SUM2,
t_icd.chinese_diag AS r_CHINESE,
t_dept.dept_name as r_DEPT_NAME
from t_all_status, t_icd,t_dept
where (t_all_status.enter_dept=3Dt_dep=ADt.dept_code)
AND (t_all_status.enter_diag not like 'M%')
AND (t_all_status.enter_diag =3D t_icd.icdcode)
group by t_dept.dept_name,t_all_status.=ADenter_diag,
t_icd.chinese_diag
order by r_SUM2 DESC=20
I really appraciate your help.
Ouyang

How to count rows in this query...

SELECT ID_AnagraficaRivendita
FROM dbo.AnagraficaRivendite
WHERE EXISTS
(SELECT *
FROM dbo.Flussi_Rivendite
WHERE dbo.Flussi_Rivendite.CodiceProdotto = 631 AND dbo.AnagraficaRivendite.ID_AnagraficaRivendita = dbo.Flussi_Rivendite.ID_AnagraficaRivendita)
AND EXISTS
(SELECT *
FROM dbo.Flussi_Rivendite
WHERE dbo.Flussi_Rivendite.CodiceProdotto = 615 AND dbo.AnagraficaRivendite.ID_AnagraficaRivendita = dbo.Flussi_Rivendite.ID_AnagraficaRivendita)
GROUP BY ID_AnagraficaRivendita

hi, in this query (in which I extract all ID_AnagraficaRivendita who have a correspondence in table Flussi_Rivendite with CodiceProdotto = 631 AND CodiceProdotto = 615), I would like to receive also a count of extracted rows... have you any idea?? Thank you ;)

There are so many ways like using COUNT(). But I always prefer usingROW_NUMBER()

e.g.

SELECT ROW_NUMBER()OVER (ORDER BY ID_AnagraficaRivenditaASC)AS Row, ID_AnagraficaRivenditaFROM dbo.AnagraficaRivenditeWHEREEXISTS(SELECT *FROM dbo.Flussi_RivenditeWHERE dbo.Flussi_Rivendite.CodiceProdotto = 631AND dbo.AnagraficaRivendite.ID_AnagraficaRivendita = dbo.Flussi_Rivendite.ID_AnagraficaRivendita)AND EXISTS(SELECT *FROM dbo.Flussi_RivenditeWHERE dbo.Flussi_Rivendite.CodiceProdotto = 615AND dbo.AnagraficaRivendite.ID_AnagraficaRivendita = dbo.Flussi_Rivendite.ID_AnagraficaRivendita)GROUP BY ID_AnagraficaRivendita
|||

try this (it was not tested but it should give you idea if it will not work)

SELECT ID_AnagraficaRivendita, FL.CodiceProdotto,count(*) Counter

FROM dbo.AnagraficaRivendite

LeftjoinFROM dbo.Flussi_Rivendite FL

ON dbo.AnagraficaRivendite.ID_AnagraficaRivendita= FLID_AnagraficaRivendita

and FL.CodiceProdottoin(631,615)

wherenot FL.CodiceProdottoisnull

groupby ID_AnagraficaRivendita, FL.CodiceProdotto

|||

Thank you for your reply, I have added ROW_NUMBER()OVER (ORDER BY ID_AnagraficaRivenditaASC)AS Row but it doesn't work, I receive an error (ADO Error: ROW_Number is not a valid function)

I don't know if is important: I am working on Sql Server 2000 and my query is in a Stored Procedure.

|||

RETURN @.@.ROWCOUNT

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

|||

row_number is new in SQL 2005, not available in SQL 2000

|||

At my home pc I have just tried instruction ROW_NUMBER on SQL Server 2005 and it runs, but maybe my question it was not clear, sorry: I need TOTAL of extracted rows, on the contray with ROW_NUMBER I receive a progressive numeration of record (if it's possible, please suggest me a solution compatible on SQL Server 2000, tooStick out tongue)

|||

Change your stored procedure into this:

SELECTCOUNT(ID_AnagraficaRivendita)AS TotalRows, ID_AnagraficaRivenditaFROM dbo.AnagraficaRivenditeWHEREEXISTS(SELECT *FROM dbo.Flussi_RivenditeWHERE dbo.Flussi_Rivendite.CodiceProdotto = 631AND dbo.AnagraficaRivendite.ID_AnagraficaRivendita = dbo.Flussi_Rivendite.ID_AnagraficaRivendita)AND EXISTS(SELECT *FROM dbo.Flussi_RivenditeWHERE dbo.Flussi_Rivendite.CodiceProdotto = 615AND dbo.AnagraficaRivendite.ID_AnagraficaRivendita = dbo.Flussi_Rivendite.ID_AnagraficaRivendita)GROUP BY ID_AnagraficaRivendita

Howevere let me also tell you thatthis is not at all an elegant solution. If you know how to work with return values, I suggest you addRETURN@.@.ROWCOUNT at the end of your original query, as suggested by DisturbedBuddha.

~

|||

thanks my friends Wink

how to count rows in field

Hello.
I built a report with one field as a group.
I want to count the number of rows in each field so I can add it to
the group field or somw where in the report.
How can I count how many rows do I have in each group?
Thanks.On May 1, 3:44 am, nicknack <roezo...@.gmail.com> wrote:
> Hello.
> I built a report with one field as a group.
> I want to count the number of rows in each field so I can add it to
> the group field or somw where in the report.
> How can I count how many rows do I have in each group?
> Thanks.
I'm not sure if it is accessible by group, but you could try using the
'rownumber' keyword. Hope this helps.
Regards,
Enrique Martinez
Sr. Software Consultant|||Hi,
If you want to show the total group by:
In the group header write a expression like
=count(Field!Yourcolumn.Value)
If you want distinct count use this expression
=countdistinct(Field!yourcolumn.value)
Regards,
Sri
"nicknack" wrote:
> Hello.
> I built a report with one field as a group.
> I want to count the number of rows in each field so I can add it to
> the group field or somw where in the report.
> How can I count how many rows do I have in each group?
> Thanks.
>|||Hi Martinez
Thanks for the replay.
I used :
=3DCountRows("GroupName")
in the group row and it does the work.
Thanks :)
EMartinez =D7=9B=D7=AA=D7=91:
> On May 1, 3:44 am, nicknack <roezo...@.gmail.com> wrote:
> > Hello.
> >
> > I built a report with one field as a group.
> > I want to count the number of rows in each field so I can add it to
> > the group field or somw where in the report.
> >
> > How can I count how many rows do I have in each group?
> >
> > Thanks.
>
> I'm not sure if it is accessible by group, but you could try using the
> 'rownumber' keyword. Hope this helps.
> Regards,
> Enrique Martinez
> Sr. Software Consultant

How to count rows ?

Hello,
i'm (sql programming newbie) goin' crazy with that:
I have several tables with a lot (10000 and more) of rows on a SQL2000
server. Before i do a select or anything else on the tables i wanna return
the number of rows via stored procedure or a function.
How can i simply count the number of rows of a table WITHOUT doing a select
or whatever before i can count the rows. In online-help i read abaout a
propert called ROWS, but how to use that ?
Any idea ?
Regards RalfWhat about SELECT COUNT(*) FROM myTable in the stored procedure you mention?
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm
"Ralf Pelzl" <ralf.pelzl@.inf.hs-anhalt.de> wrote in message
news:#uyZmzNVGHA.5148@.TK2MSFTNGP12.phx.gbl...
> Hello,
> i'm (sql programming newbie) goin' crazy with that:
> I have several tables with a lot (10000 and more) of rows on a SQL2000
> server. Before i do a select or anything else on the tables i wanna return
> the number of rows via stored procedure or a function.
> How can i simply count the number of rows of a table WITHOUT doing a
select
> or whatever before i can count the rows. In online-help i read abaout a
> propert called ROWS, but how to use that ?
> Any idea ?
> Regards Ralf
>|||I have seen in the past that there is a way to get the full table count
out of the master database, thus being much quicker than actually
counting the whole table. However not only is that not a great idea for
a newbie to be trying, but also I can't remember how to do it.
Out of interest, why do you need to count your tablerows before you do
anything with them?|||> How can i simply count the number of rows of a table WITHOUT doing a
> select
You will need to perform a select of some kind.
You can try
select rowcnt from sysindexes where id = OBJECT_ID('tablename') and indid in
(0,1)
However, this will depend on current sysindexes data (e.g. by running DBCC
UPDATEUSAGE). If you don't update usage first, then hopefully you can rely
on potentially inaccurate numbers. Otherwise, SELECT COUNT(*) FROM
tablename is the only accurate way.
A|||Hello
thanks for the quick answer. The reason to read out the rowcount is that i
write a .NET-Application where i read parts of datarows into the
application, work with it and update it in the database. This progress will
take a while so i wanna add a progressbar. The only way (i know) to display
a progressbar is to know the last record number to set the min=0 and the
max=rowcount.
Regards
Ralf
"Will" <william_pegg@.yahoo.co.uk> schrieb im Newsbeitrag
news:1143822887.413725.285180@.e56g2000cwe.googlegroups.com...
>I have seen in the past that there is a way to get the full table count
> out of the master database, thus being much quicker than actually
> counting the whole table. However not only is that not a great idea for
> a newbie to be trying, but also I can't remember how to do it.
> Out of interest, why do you need to count your tablerows before you do
> anything with them?
>|||Hello,
thanks for the answer. I want create a function i can use for more than one
table. But i dont know a way to give this function the tablename as
parameter. I tried that:
create function [dbo].[getRowCountOf] (@.tableName nvarchar(20)
returns integer
AS
return select count(*) from @.tableName
"Jack Vamvas" <delete_this_bit_jack@.ciquery.com_delete> schrieb im
Newsbeitrag news:KoidnbNxSocEyLDZRVnysw@.bt.com...
> What about SELECT COUNT(*) FROM myTable in the stored procedure you
> mention?
> --
> Jack Vamvas
> ___________________________________
> Receive free SQL tips - www.ciquery.com/sqlserver.htm
> "Ralf Pelzl" <ralf.pelzl@.inf.hs-anhalt.de> wrote in message
> news:#uyZmzNVGHA.5148@.TK2MSFTNGP12.phx.gbl...
> select
>|||sorry, i send to fast...
sql means, that i have to declare @.tableName.
Is there a way to make the tablename variable in a function ?
Regards
Ralf
"Ralf Pelzl" <ralf.pelzl@.inf.hs-anhalt.de> schrieb im Newsbeitrag
news:udBe4lOVGHA.5004@.TK2MSFTNGP11.phx.gbl...
> Hello,
> thanks for the answer. I want create a function i can use for more than
> one table. But i dont know a way to give this function the tablename as
> parameter. I tried that:
> create function [dbo].[getRowCountOf] (@.tableName nvarchar(20)
> returns integer
> AS
> return select count(*) from @.tableName
> "Jack Vamvas" <delete_this_bit_jack@.ciquery.com_delete> schrieb im
> Newsbeitrag news:KoidnbNxSocEyLDZRVnysw@.bt.com...
>|||DECLARE @.tablename SYSNAME;
SET @.tablename = N'<enter table here>';
SELECT RowCnt FROM sysindexes WHERE id = OBJECT_ID(@.tablename) AND indid IN
(0,1);
Otherwise, see
http://www.sommarskog.se/dynamic_sql.html
"Ralf Pelzl" <ralf.pelzl@.inf.hs-anhalt.de> wrote in message
news:eD6O8nOVGHA.1868@.TK2MSFTNGP09.phx.gbl...
> sorry, i send to fast...
> sql means, that i have to declare @.tableName.
> Is there a way to make the tablename variable in a function ?
> Regards
> Ralf
> "Ralf Pelzl" <ralf.pelzl@.inf.hs-anhalt.de> schrieb im Newsbeitrag
> news:udBe4lOVGHA.5004@.TK2MSFTNGP11.phx.gbl...
>|||Hello,
thanks for answer. I tried this:
create function [dbo].[getRowCountOf] (@.tableName nvarchar(20)
returns integer
as
return select count(*) from @.tableName
But it does'nt work because sql means i have to declare @.tableName. Do You
know how i can make the tablename variable ? Otherwise i will use your hint:
create function [dbo].[getRowCountOf] (@.tableName nvarchar(20)
returns table
as
return select rowcnt from sysindexes where id = OBJECT_ID('(@.tableName') and
indid in (0,1)
I've tried it and it works :-)
Regards
Ralf
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> schrieb im
Newsbeitrag news:%23b5lwGOVGHA.2704@.tk2msftngp13.phx.gbl...
> You will need to perform a select of some kind.
> You can try
> select rowcnt from sysindexes where id = OBJECT_ID('tablename') and indid
> in (0,1)
> However, this will depend on current sysindexes data (e.g. by running DBCC
> UPDATEUSAGE). If you don't update usage first, then hopefully you can
> rely on potentially inaccurate numbers. Otherwise, SELECT COUNT(*) FROM
> tablename is the only accurate way.
> A
>|||By the way, without quotas arount @.tableName
"Ralf Pelzl" <ralf.pelzl@.inf.hs-anhalt.de> schrieb im Newsbeitrag
news:elwIwxOVGHA.1572@.tk2msftngp13.phx.gbl...
> Hello,
> thanks for answer. I tried this:
> create function [dbo].[getRowCountOf] (@.tableName nvarchar(20)
> returns integer
> as
> return select count(*) from @.tableName
> But it does'nt work because sql means i have to declare @.tableName. Do You
> know how i can make the tablename variable ? Otherwise i will use your
> hint:
> create function [dbo].[getRowCountOf] (@.tableName nvarchar(20)
> returns table
> as
> return select rowcnt from sysindexes where id = OBJECT_ID('(@.tableName')
> and indid in (0,1)
> I've tried it and it works :-)
> Regards
> Ralf
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> schrieb im
> Newsbeitrag news:%23b5lwGOVGHA.2704@.tk2msftngp13.phx.gbl...
>

How to count number of rows

Hello, Dean
Try this:

select distinct c1, c2 into #tmp_1 from t1
select count(*) as cnt from #tmp_1
drop table #tmp_1

With best regards.how to count number of rows?

well, try this...

select count(*)
from <yourtable>

How to count number of Groups in MS-SQL ?

The following SQL works on Access and Oracle to return the number ofGroups(Rows) of the SQL. In MS-SQL this SQL is not valid. What is theequivalent in MS-SQL?
Select Count(1) FROM (Select ShipRegion, Sum(FREIGHT) as [TotalFreight]from ORDERS WHERE OrderID < 123456 GROUP BY ShipRegion )
Thanks,

Frankk
Hello,
this seems to work:
> Select Count(1) FROM (Select ShipRegion, Sum(FREIGHT) as [TotalFreight] from ORDERS WHERE OrderID < 123456 GROUP BY ShipRegion) AS X

Hope this helps. -LV
|||LV,
Thanks, it works !!!!!

Frankk

How To Count how many ROWS are returned in SQL Data Reader

My code keeps reading for another row when none are present....

There's are only unique records in the dB so far, but dupes are possible

While reader.has rows

reader.read

if value not db.null

get values

end if

end while

The program gets the row and reads it. After processing the values, it goes to thru the While and reads the next row (none), it then crashes on if value not db.null... because the value is null or no record. The dB has Values Required option on - no empty fields allowed.

Is there a way to count the ROWS returned?

Thanks.

That depends on your coding language, which one are you using ? For example the .NET Framework has the read() method which returns false if no record can be read anymore, leading to the following code:

while dr.Read()
{}

and to the fact that you won′t get an error if no rows is read anymore.

In my good old ASP ages there was something similar like rs.EOF or something, I guess there has to be be something in your coding language too.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

I'm using VB.

But I'm using the WHILE there are ROWS, READ, so it shouldn't read if no more rows are present. Only ONE ROW is returned, so the WHILE statement should return FALSE on the second run of the loop and exit the LOOP because no more ROWS are present.

Moverover, it continued to READ, so perhaps the framework (NET) detected another row (empty) and reads it (while rows are present, read). Howbeit, this ROW is empty so an error is generated at the if not null.db line - but even so, it should NOT generate an error because NOT db.Null should detect that there is a Null Value and return FALSE and exit the loop.

Sunday, February 19, 2012

how to copy one table from one database to another on different servers?

Hello.

I need to copy all of the rows in a table from a database on one server, to another existing table of the same name in a different database on a different server. I'm trying to use a SELECT INTO statement. Any idea how to do this?

I've tried

SELECT * INTO DestinationServer.dbo.DestinationDB.DestinationTable
FROM SourceTable AS SourceTable_1


But this doesn't work, saying there are too many prefixes.

Any idea how to do this?

If you are using SQL2000, you use DTS to copy the table or SSIS if using SQL2005. Alternatively you can use OPENROWSET in the target database to read the table in the source database.

|||

(1) SELECT .. INTO is different from INSERT INTO SELECT.

SELECT INTO tries to create the table in the INTO clause and then inserts the data from the SELECT into the table.

INSERT INTO SELECT does not create the destination table. It will directly try to insert the result of the SELECT into the destination table.

(2) The naming convention you have "DestinationServer.dbo.DestinationDB.DestinationTable" is incorrect. its ServerName.Database.dbo.Table. You have it the other way.

Depending on whether you are executing this from source or destination you have to use appropriate 4-part name (basically for whichever (source/target) is remote).

(3) Spell out all the column names if you are using INSERT INTO SELECT.

|||

Thanks guys. It's all working now.

|||how to copy one table from one database to another on different servers?