Showing posts with label order. Show all posts
Showing posts with label order. Show all posts

Monday, March 12, 2012

Generating a series of numbers or dates?

I want to generate a resultset that is just a series of numbers in
ascending order or perhaps a series of dates.. What I mean is, is
there a way to generate a temporary table of dates given an input of a
start date and an end date.. This table would then contain an entry
for each date in ascending order from the start date to and including
the end date..

Or perhaps with numbers, given a start of 5 and and end of 7
the resulting table would be

5
6
7

Would appreciate any help with this one.. Thanks
ChrisThe usual technique is just to keep these as permanent tables in your
database and then SELECT numbers and dates out of them as required.

Dates:

CREATE TABLE Calendar
(caldate DATETIME NOT NULL PRIMARY KEY)

INSERT INTO Calendar (caldate) VALUES ('20000101')

WHILE (SELECT MAX(caldate) FROM Calendar)<'20101231'
INSERT INTO Calendar (caldate)
SELECT DATEADD(D,DATEDIFF(D,'19991231',caldate),
(SELECT MAX(caldate) FROM Calendar))
FROM Calendar

Numbers:

CREATE TABLE Numbers
(num INTEGER PRIMARY KEY)

INSERT INTO Numbers VALUES (1)

WHILE (SELECT MAX(num) FROM Numbers)<8192
INSERT INTO Numbers
SELECT num+(SELECT MAX(num) FROM Numbers)
FROM Numbers

--
David Portas
----
Please reply only to the newsgroup
--|||Refer to:
http://www.bizdatasolutions.com/tsql/tblnumbers.asp

--
-- Anith
( Please reply to newsgroups only )

Generate Xml in hierarchical order from DB

Hi

I have a table named UserProfile with following structure :

(TypeIdbigint,TypeNamenvarchar(100),ParentTypeIdbigint)

Have some following sample data :

TypeId TypeName ParentTypeId

1User0

2Artist1

3Singer2

4DJ1

5Band1

6Partner0

7Movies6

8GoodMovies7

9BadMovies7

10Producer6

I want to generate the XML in following form

<Main>

<TypeId>1</TypeId>

<TypeName>User</TypeName>

<ParentTypeId>0</ParentTypeId>

<SubProfiles>

<SubProfile>

<TypeId>2</TypeId>

<TypeName>Artist</TypeName>

<ParentTypeId>1</ParentTypeId>

</SubProfile>

<SubProfile>

<TypeId>4</TypeId>

<TypeName>DJ</TypeName>

<ParentTypeId>1</ParentTypeId>

</SubProfile>

<SubProfile>

<TypeId>5</TypeId>

<TypeName>Band</TypeName>

<ParentTypeId>1</ParentTypeId>

</SubProfile>

</SubProfiles>

</Main>

I am usign CTE for recursive query and then using for xml auto mode to generate xml. But the data is not generating when the parent child relation is in depth level.

Kindly anybody tell me the query so that I can generate the xml in heirarchical order of data.

CREATE FUNCTION dbo.GetProfileSubTree(@.TypeId int)
RETURNS XML
BEGIN RETURN
(SELECT TypeId AS "TypeId",
TypeName AS "TypeName",
ParentTypeId AS "ParentTypeId",
dbo.GetProfileSubTree(TypeId)
FROM UserProfile
WHERE ParentTypeId=@.TypeId
ORDER BY TypeId
FOR XML PATH('SubProfile'),ROOT('SubProfiles'),TYPE)
END

GO

SELECT TypeId AS "TypeId",
TypeName AS "TypeName",
ParentTypeId AS "ParentTypeId",
dbo.GetProfileSubTree(TypeId)
FROM UserProfile
WHERE ParentTypeId=0
ORDER BY TypeId
FOR XML PATH(''),ROOT('Main'),TYPE

|||

Hi,

This solution helps me to generate the heirarchical data

Thanks

|||

Note that SQL Server 2005 has a maximum limit of 32 recursively nested function invocations.

This is mentioned here at

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

Can you suggest any alternate method for unlimited depth?

Generate Xml in hierarchical order from DB

Hi

I have a table named UserProfile with following structure :

(TypeId bigint,TypeNamenvarchar(100),ParentTypeIdbigint)

Have some following sample data :

TypeId TypeName ParentTypeId

1User0

2Artist1

3Singer2

4DJ1

5Band1

6Partner0

7Movies6

8GoodMovies7

9BadMovies7

10Producer6

I want to generate the XML in following form

<Main>

<TypeId>1</TypeId>

<TypeName>User</TypeName>

<ParentTypeId>0</ParentTypeId>

<SubProfiles>

<SubProfile>

<TypeId>2</TypeId>

<TypeName>Artist</TypeName>

<ParentTypeId>1</ParentTypeId>

</SubProfile>

<SubProfile>

<TypeId>4</TypeId>

<TypeName>DJ</TypeName>

<ParentTypeId>1</ParentTypeId>

</SubProfile>

<SubProfile>

<TypeId>5</TypeId>

<TypeName>Band</TypeName>

<ParentTypeId>1</ParentTypeId>

</SubProfile>

</SubProfiles>

</Main>

I am usign CTE for recursive query and then using for xml auto mode to generate xml. But the data is not generating when the parent child relation is in depth level.

Kindly anybody tell me the query so that I can generate the xml in heirarchical order of data.

CREATE FUNCTION dbo.GetProfileSubTree(@.TypeId int)
RETURNS XML
BEGIN RETURN
(SELECT TypeId AS "TypeId",
TypeName AS "TypeName",
ParentTypeId AS "ParentTypeId",
dbo.GetProfileSubTree(TypeId)
FROM UserProfile
WHERE ParentTypeId=@.TypeId
ORDER BY TypeId
FOR XML PATH('SubProfile'),ROOT('SubProfiles'),TYPE)
END

GO

SELECT TypeId AS "TypeId",
TypeName AS "TypeName",
ParentTypeId AS "ParentTypeId",
dbo.GetProfileSubTree(TypeId)
FROM UserProfile
WHERE ParentTypeId=0
ORDER BY TypeId
FOR XML PATH(''),ROOT('Main'),TYPE

|||

Hi,

This solution helps me to generate the heirarchical data

Thanks

|||

Note that SQL Server 2005 has a maximum limit of 32 recursively nested function invocations.

This is mentioned here at

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

Can you suggest any alternate method for unlimited depth?

Generate view and stored proc scripts retaining depandancy order

Hi,
We need to generate view and stored proc scripts so that they are
generated in the right order. eg
ViewA uses ViewB
Generate
--
ViewB
ViewA
etc
When using Enterprise Manager it just dumps the views and stored procs
alphabetically.
regards
PaulUsing EM, there is no guarantee that it'll generate the SP scripts in the
correct dependency order. To be sure, you need to sort then generated
scripts yourself.
I do this by using a script to scanning the SP scripts, find all the
immediate dependencies, and sort the SP by depdendency.
--
Linchi Shea
linchi_shea@.NOSPAMml.com
"Paul Sjoerdsma" <paul.sjoerdsma@.redsnapper.nl> wrote in message
news:MPG.19e266ada9d972919896c3@.127.0.0.1...
> Hi,
> We need to generate view and stored proc scripts so that they are
> generated in the right order. eg
> ViewA uses ViewB
> Generate
> --
> ViewB
> ViewA
> etc
> When using Enterprise Manager it just dumps the views and stored procs
> alphabetically.
> regards
> Paul

Friday, March 9, 2012

Generate SQL script which includes table structure & data?

What are the steps that I should follow in order to generate sql script which includes both the table structure and data?

Thanks

Dear perpetualdream,

Use SQL Server Database Publishing Wizard Smile

HTH,

Suprotim Agarwal

|||

thanks... great tool

Generate SQL script order by table name

I use SQL Server 2005 scripts wizard to generate scripts for database. Is there any ways it generate script in the order of table name?

I need to compare all data tables between two databases. It is a crazy job if I compare one table by one table.

If I can generate database script in the order of table name, which will make my job easy a lot.

Thanks for any advise.

I'd recommend getting visual studio team edition for database professionals. It can do schema and data comparisons with a nice user interface.

If that's not an option, I'm sure you could find a free file compare utility on the web, so you can stop comparing scripts manually.

|||

I believe thats best option to do for a comparison of the objects within the database.

But there is no such option within generate scripts wizard for the table order, you have to edit the content once it is generated.

|||AnthonyMartin: My guess is that the original poster _is_ using a file comparison utility. The problem is when the order of the tables are rearranged, it looks like huge chunks have changed when in fact the only thing that changed was the order the objects were scripted.

I'm having the same problem as the original poster. Even when comparing a script generated from the same database on the same server from two different dates, the order that the objects are scripted will jump around, even for objects that weren't changed at all since the last time they were scripted.

In my case, I'm not particular about how the objects are sorted as long as it is consistent! I want to be able to compare the script from say two months ago and find the one table that had a single column altered in a database containing dozens of tables, stored procedures etc - the proverbial needle in a haystack. A tool like Beyond Compare will do this quite well, but has difficulties when the order of the objects is inconsistent.

Generate SQL script and columns order

I am using Generate SQL script or scptxft tool for generartion creation
script for my database.
But unfortunally this script generates order for columns in creating
table not saticfying me. I need to have order exactly like I see in
Enterprise Manager, but the default order is other...
Can anybode give an advise how solve this problem?
Hi,
I think you can not.
Try doing a reverse engineering if you have Erwin data modeller or else you
have to arrange manually.
Thanks
Hari
SQL Server MVP
"Sergi Adamchuk" <adamchuk@.gmail.com> wrote in message
news:1126006864.707536.128230@.o13g2000cwo.googlegr oups.com...
>I am using Generate SQL script or scptxft tool for generartion creation
> script for my database.
> But unfortunally this script generates order for columns in creating
> table not saticfying me. I need to have order exactly like I see in
> Enterprise Manager, but the default order is other...
> Can anybode give an advise how solve this problem?
>
|||Sergi Adamchuk wrote:
> I am using Generate SQL script or scptxft tool for generartion
> creation script for my database.
> But unfortunally this script generates order for columns in creating
> table not saticfying me. I need to have order exactly like I see in
> Enterprise Manager, but the default order is other...
> Can anybode give an advise how solve this problem?
I just did a little digging into the SQL EM Generate SQL Scripts and the
Design Table functions to see how they were querying the list of columns
in the tables for display.
It appears they are both using the sp_MShelpcolumns system procedure
with the OrderBy column specified as 'id'. With that parameter, you
should be seeing the order of the columns as they are ordered by their
ColID in syscolumns.
I tested on SQL Server 2000 SP4 (Server and Client Tools). If you are
not seeing the same result from those two functions, can you tell me:
1- Are you running SP4 on the server and/or the client
2- What do you see as the column order when you query the syscolumns
table for the table in question and order by the ColID column? Which
version of the output is correct and which is incorrect?
You can use:
Select name from syscolumns where id = object_id('<table_name>') order
by colid
David Gugick
Quest Software
www.imceda.com
www.quest.com

Sunday, February 19, 2012

generate all possible subsets from master set?

Hi.

I have a master set of values, lets say

(1,2,3)

for example. I want to use T-sql to generate all possible subsets of this master set. Order of values is unimportant, what I want is unique sets, i.e.

(1)
(2)
(3)
(1,2)
(1,3)
(2,3)
(1,2,3)

thx.Can I ask why?

In a table or a string?

I'm thinking CROSS JOIN...|||Back up a sec', manster.

Do 1, 2, and 3 represent columns? In that case, you would write a cross-join query as suggested by Brett.

But if 1, 2, 3, ... to N represent values in a single column, and you want all the permutations, you will need to write a stored proc that loops through the dataset N times.

blindman|||thanks, all.

Cross join, most likely. I'm working this up from scratch to complete a current project. The higher-ups are handing me a list of fields from our DB, fifteen max, and then asking for all possible subsets of these fifteen, after which we test the subsets to see which offer the most "value."

if someone can offer a cross join example using numbers 1,2,3 as above, that would be great. I'll just key the number back to the actual field names.

thx.|||That sounds bizzare!

Can you post the DDL of the table?

And some sample data?

And are you're higher ups high?|||yes, I know it sounds bizarre, but you'd have to see the data in the table to understand why they're asking for this, and I can't show it.

bottom line is this: I have a list of 15 fields in a single table and I need to generate all possible subsets of these 15 fields.

table1

F1 F2 F3 etc.

output is all possible subsets of these fields:

F1
F2
F3
F1 F2
F2 F3
F1 F3
F1 F2 F3

thx!|||Have a look at Arnolds Reply...you'd need to marry rows to numbers somehow

You'll also need
CREATE TABLE Numbers(n int)
GO
DECLARE @.x int
SET NOCOUNT ON
SELECT @.x = 1
WHILE @.x < 101 BEGIN
INSERT INTO Numbers (n) SELECT @.x
SELECT @.x = @.x + 1
END
SET NOCOUNT OFF

To play with it...vary cool though...

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=30752|||What kind of processing power are your higher-ups prepared to pay for?

...because you are looking at something over a QUADRILLION permutations, depending on how many values are in your columns.
Start it running, and then go get yourself a cup of coffee...in Brazil.

blindman|||I'm sorry if I haven't been clear on this. I only need all possible combinations of the FIELD NAMES, not the actual per-record data in the fields.

Once I get all possible combinations of the field names, I can then look at which fields I'll actually use in succeeding queries.

thx|||Originally posted by blindman
What kind of processing power are your higher-ups prepared to pay for?

...because you are looking at something over a QUADRILLION permutations, depending on how many values are in your columns.
Start it running, and then go get yourself a cup of coffee...in Brazil.

blindman

LOL...

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=30646

Some Calculations here...

Say we have a table of 10,000 measly rows...

You want to join col1 to each of the other 14 columns to represent permutations

OK Col1 + Col2 = 100,000

So i't like Col1 , Row 1 + Col2, Every row

Do that 14 more times for all the other columns...1.4 million rows

And we haven't yet begun! That's still just 2 dimensions of the data

Now lets see, Three dimensions

I guess that would be Col1, Row 1, Col2, Row 1, Col3, All the Rows
Then Col1, Row1, Col2, Row2, Col3 All the rows of data

So what that would be 10,000 * 10,000 * 10,000

Blindman help me out here, sound right?

The for all other cols * 14, or 14 million?

4 Dimensions...

140,000,000? Just a guess...

That about right?|||here's a link to a web page that does what I'm trying to do, but this routine truncates at 500 entries returned...

mine is n=15, k=15

and I'm looking for lex order, list of elements.

http://www.theory.cs.uvic.ca/~cos/gen/comb.html|||Originally posted by manster
I'm sorry if I haven't been clear on this. I only need all possible combinations of the FIELD NAMES, not the actual per-record data in the fields.

Once I get all possible combinations of the field names, I can then look at which fields I'll actually use in succeeding queries.

thx

Was busy doing calcs when you posted...

Are you trying to build a "pick list" of what fields you want to select for an end user..

Even still that's a lot of combinations..its 15 factorial..more actually the way you want it..

DECLARE @.x BIGINT
SELECT @.x = 1*2*3*4*5*6*7*8*9*10*11*12--*13*14*15
SELECT @.x

I get an arithmetic overflow at 13...|||I guess you could call it a pick list as long as all subsets are represented.

I'm looking back at the original field list and see that the main required fields number about 10, so 15 was an over-estimation on my part.

thx.|||But that's only bit data...is that what you're looking for?|||yes, just the subsets taken from the "master" list of fields. I'm only interested in the fieldname subsets, not the records in the db at this point.|||You are still talking about over 4 million permutations.

blindman