Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
7/21/2005 6:13:27 AM EDT
When writing a query in oracle, you can replace a table name in the from clause with another query, e.g.:

Select  * From
    (select * from table where rownum < 100)
where rownum < 50
;



Do you know if you can do the same sort of thing in Sql Server 2000?


If I could, I'd just create a temporary table and select from that, but the retarded DBA refuses to give us create table abilities.


7/21/2005 7:45:50 AM EDT
[#1]
Absolutely.

MS & Oracle are relatively similar in their SELECT statements.  The only thing is MS does not (that I can recall) have a built in rownum on result sets like Oracle does.
7/21/2005 8:40:33 AM EDT
[#2]
You can do what I often do -- write dynamic SQL statements.

Such as:

declare @sql varchar(500)
declare @tablename varchar(50)

select @tablename ='TestTable'

select @sql = 'select * from ' + @tablename

exec(@sql)

It won't have the speediest performance in the world, but you can build any sort of query you want that way.

BTW, been there, done that myself with stupid DBAs.  "Here, put these handcuffs on and go to work."
7/21/2005 8:52:41 AM EDT
[#3]
Yes you can do that in T-SQL...
7/21/2005 10:16:50 AM EDT
[#4]
Strange, here's the code I'm trying to run just as an example of doing this:

----------------------------------------------------
select  * from
(select * from TABLE  where case between 100 and 200)
where   case = 150
---------------------------------------------------



I get back the following message:


Server: Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'where'.


Any ideas?





7/21/2005 10:18:58 AM EDT
[#5]

Quoted:
"Here, put these handcuffs on and go to work."



That's a very good description of the bs being foisted upon us by the new dba.
7/21/2005 10:25:23 AM EDT
[#6]
SQL 2K has an undocumented stored proc called 'sp_msforeachtable' which might work for you.
www.databasejournal.com/features/mssql/article.php/3441031

sp_msforeachtable 'select * from ?'

The question mark is a placeholder for the table name.

If you need more granularity, you'd need to look into the sysobjects table, a cursor and some dynamically built and executed SQL statements.
7/21/2005 10:27:58 AM EDT
[#7]

Quoted:
If I could, I'd just create a temporary table and select from that, but the retarded DBA refuses to give us create table abilities.




SQL also has table variables which should get you around that.  For example...


declare @MyTempTable table ( FirstName varchar(30), LastName varchar(30) )

insert into @MyTempTable values('John', 'Doe')

select * from @MyTempTable

FirstName                      LastName                      
------------------------------ ------------------------------
John                           Doe

(1 row(s) affected)

7/21/2005 10:45:04 AM EDT
[#8]

Quoted:

Quoted:
If I could, I'd just create a temporary table and select from that, but the retarded DBA refuses to give us create table abilities.




SQL also has table variables which should get you around that.  For example...


declare @MyTempTable table ( FirstName varchar(30), LastName varchar(30) )

insert into @MyTempTable values('John', 'Doe')

select * from @MyTempTable

FirstName                      LastName                      
------------------------------ ------------------------------
John                           Doe

(1 row(s) affected)




i it possible to get around having to provide the column names and data types for "@MyTempTable"?





7/21/2005 10:45:25 AM EDT
[#9]

Quoted:

Quoted:
If I could, I'd just create a temporary table and select from that, but the retarded DBA refuses to give us create table abilities.




SQL also has table variables which should get you around that.  For example...


declare @MyTempTable table ( FirstName varchar(30), LastName varchar(30) )

insert into @MyTempTable values('John', 'Doe')

select * from @MyTempTable

FirstName                      LastName                      
------------------------------ ------------------------------
John                           Doe

(1 row(s) affected)




If you can go that far, just do:

SELECT * INTO #TempTable.  That's a memory only temporary table that lives as long as the connection/transaction does.


As for the error you're getting, I believe you have to declare the table as an alias.  eg:

SELECT * FROM (SELECT * FROM TABLE) AS TABLE1 WHERE CASE BETWEEN 100 AND 150
7/21/2005 10:47:53 AM EDT
[#10]
what I want to get as close as possible to doing is


create table temp as
select * from table where ...
;

select * from temp where ...
;

drop table temp
;
7/21/2005 10:50:13 AM EDT
[#11]

Quoted:

Quoted:

Quoted:
If I could, I'd just create a temporary table and select from that, but the retarded DBA refuses to give us create table abilities.




SQL also has table variables which should get you around that.  For example...


declare @MyTempTable table ( FirstName varchar(30), LastName varchar(30) )

insert into @MyTempTable values('John', 'Doe')

select * from @MyTempTable

FirstName                      LastName                      
------------------------------ ------------------------------
John                           Doe

(1 row(s) affected)




If you can go that far, just do:

SELECT * INTO #TempTable.  That's a memory only temporary table that lives as long as the connection/transaction does.


As for the error you're getting, I believe you have to declare the table as an alias.  eg:

SELECT * FROM (SELECT * FROM TABLE) AS TABLE1 WHERE CASE BETWEEN 100 AND 150




you're right on the error, that's what it was.

You don't need to provide an alias to do the same thing in oracle.


7/21/2005 10:50:25 AM EDT
[#12]

Quoted:
As for the error you're getting, I believe you have to declare the table as an alias.  eg:

SELECT * FROM (SELECT * FROM TABLE) AS TABLE1 WHERE CASE BETWEEN 100 AND 150



Tried it and that doesn't work....

select * from (select name from sysobjects where type = 'U') as MyTable

Theoretically, that should select * from all tables based on the user-defined tables in the database, but it just returns me a list of tables.  I just don't think it's possibly to use table aliases from queries.
7/21/2005 10:53:43 AM EDT
[#13]

Quoted:

Quoted:

Quoted:
If I could, I'd just create a temporary table and select from that, but the retarded DBA refuses to give us create table abilities.




SQL also has table variables which should get you around that.  For example...


declare @MyTempTable table ( FirstName varchar(30), LastName varchar(30) )

insert into @MyTempTable values('John', 'Doe')

select * from @MyTempTable

FirstName                      LastName                      
------------------------------ ------------------------------
John                           Doe

(1 row(s) affected)




If you can go that far, just do:

SELECT * INTO #TempTable.  That's a memory only temporary table that lives as long as the connection/transaction does.


As for the error you're getting, I believe you have to declare the table as an alias.  eg:

SELECT * FROM (SELECT * FROM TABLE) AS TABLE1 WHERE CASE BETWEEN 100 AND 150




cool, I can do that.

What happens if the query you're using to fill #temptable returns more data than can be stored in memory?

7/21/2005 10:56:27 AM EDT
[#14]

Quoted:

Quoted:
As for the error you're getting, I believe you have to declare the table as an alias.  eg:

SELECT * FROM (SELECT * FROM TABLE) AS TABLE1 WHERE CASE BETWEEN 100 AND 150



Tried it and that doesn't work....

select * from (select name from sysobjects where type = 'U') as MyTable

Theoretically, that should select * from all tables based on the user-defined tables in the database, but it just returns me a list of tables.  I just don't think it's possibly to use table aliases from queries.



no, it works just fine.

What you did is

(select name from sysobjects where type = 'U') as MyTable

so you'd be getting a list of names and calling it mytable

then you'd be selecting * from mytable, which would return the list of names.

7/21/2005 11:07:55 AM EDT
[#15]

Quoted:
Strange, here's the code I'm trying to run just as an example of doing this:

----------------------------------------------------
select  * from (select * from TABLE  where case between 100 and 200) where case = 150
---------------------------------------------------

I get back the following message:

Server: Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'where'.



My first thought is that your first select doesn't know what "case" is, so it barfs on the "where case = 150" part.  Does the "main" query know the column names in the table used in the "inner" query?  I wouldn't think that data would be within the scope of the main query.
7/21/2005 11:08:21 AM EDT
[#16]
Ah, I get you now!  Which begs the question, why do you need the subquery?

Select * From
(select * from table where rownum < 100) as Table1
where rownum < 50

Wouldn't this do the same?

select * from table where rownum < 50


Curious to know cause I've never needed to do something like that, but it may come in handy in the future.  




7/21/2005 11:20:49 AM EDT
[#17]

Quoted:
Ah, I get you now!  Which begs the question, why do you need the subquery?

Select * From
(select * from table where rownum < 100) as Table1
where rownum < 50

Wouldn't this do the same?

select * from table where rownum < 50


Curious to know cause I've never needed to do something like that, but it may come in handy in the future.  








I wouldn't need it in that example. I was just making something up to illustrate what I was trying to do.

For the real version, the query I put in the "from" clause will be a bit more complicated.

7/21/2005 11:25:36 AM EDT
[#18]
Subqueries are the devil.
7/21/2005 12:10:27 PM EDT
[#19]

Quoted:
Subqueries are the devil.



not being given a chunk of disk space and create table permission is the devil

7/21/2005 12:37:27 PM EDT
[#20]

Quoted:

cool, I can do that.

What happens if the query you're using to fill #temptable returns more data than can be stored in memory?




I believe the table is still server side so you're only limited by the amount of memory allocated to MSSQL (if any) or ultimately the server's memory limit.

If you're processing THAT many rows by this thing, your DBA needs to pull the stick out of his ass and figure a better way to do all this.

Also realize that the #temptable  (the # is what makes it the temp table BTW, if you use ## it makes it global temporary) can be filled with any SELECT including JOINS so you're not limited by a single table, it will fill with the entire results set and will take on the column defs of the selected table(s).

If you need to bounce an idea around just IM me.
7/21/2005 12:51:28 PM EDT
[#21]

Quoted:

Quoted:
What happens if the query you're using to fill #temptable returns more data than can be stored in memory?



I believe the table is still server side so you're only limited by the amount of memory allocated to MSSQL (if any) or ultimately the server's memory limit.




# temp tables are stored in the tempdb database so you're only limited by how large that database can grow.  However, like Benny said, if the temp table is going to grow the temdb too large, a different solution maybe be in order.
7/21/2005 1:08:36 PM EDT
[#22]

Quoted:
# temp tables are stored in the tempdb database so you're only limited by how large that database can grow.  However, like Benny said, if the temp table is going to grow the temdb too large, a different solution maybe be in order.



D'oh, I forgot about tempdb.  Good call.

Yes, that's your limit.  And if that fills up you've got bigger problems than a DBA with a superiority complex.
7/21/2005 1:10:01 PM EDT
[#23]
I think I follow what you are doing:  Here's an example of what I think will do it for you

NOTE: Imaginary tables and such, find all employees that have had diciplinary action.

Table schema:

Users:  L_Name, F_Name, Employee_id

Discipline: Employee_id, Action, Description

Select L_name, F_name, Employee_id where Employee_id in (Select Employee_id from Discipline)

If that's not what you're looking for, lemme know.  If you are looking for a particluar ROW number, search Help for the FETCH command.

7/21/2005 1:25:50 PM EDT
[#24]

Quoted:

If you're processing THAT many rows by this thing, your DBA needs to pull the stick out of hisher ass and figure a better way to do all this.




I think this woman's philosophy is to minimize the amount of work she or her staff could possibly be asked to do ... as opposed to making things work as well as possible for her customers.



The #table  temp table thing is working pretty well. I put a shitload of records in there amounting to about 4 gigs of data and querying from it seemed to work well.