SunQuest
           MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 25th, 2004, 03:36 AM
tjbyrne tjbyrne is offline
PLEASE HELP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 11 tjbyrne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question CANT USE A VARIABLE INSTEAD OF TABLE NAME (eg From @var)

Hi every1,

I’ve got a DB with roughly 90 tables, roughly half of these tables have a column called “last_date”. I want to find find the last “last_date” in each table that has this coulmn but only the tables that contain it. I have tried using
Code:
exec sp_MSforeachtable 'Select max(last_date) from ?'
which does return all the “last_date” but as checks ALL TABLES (not just the ones with the “last_date” column) it gives me an error for all the table it checks which doesn’t have that column

(I KNOW ITS LONG ....GO ON JUST A BIT LONGER )

Have also tried cursors using “ select object_name(id) from syscolumns where patindex('LAST_DATE',name) <> 0 ” which returns a list of all the tables with “last_date” in it. This won’t work as I cant use a variable (@cur_tab_name ) instead of the table name (def 1 of the most annoying things about SQL) “select max(LAST_DATE) from @cur_tab_name

Code:
DECLARE @cur_tab_name varchar(30)
DECLARE  curTAB
CURSOR FOR

	select object_name(id)     
             from syscolumns
	where patindex('LAST_DATE',name) <> 0	
	OPEN curTABLE

	FETCH next from curTABLE INTO @cur_tab_name	
	BEGIN
                  select max(LAST_DATE) from @cur_tab_name --WRONG
	END

	WHILE (@@FETCH_STATUS =0)

	BEGIN
		FETCH curTABLE INTO @cur_tab_name	
	END

	CLOSE curTABLE
	DEALLOCATE curTABLE

ANY HELP WOULD BE REALLY APPRECIATED
Thanks,
tjbyrne

Last edited by tjbyrne : March 25th, 2004 at 07:39 AM.

Reply With Quote
  #2  
Old April 28th, 2004, 05:05 PM
rongower rongower is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 rongower User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Using parameter for "FROM" statement

Here is some sample code that aggregates data from several tables with the same structure into one table. A previous comment suggested that you should never need to do anything like this. However, in the real world where everything does not fit nicely into a box as in this example, it is necessary to find a solution like this. Let's all try to be a little more open and willing to solve problems rather than saying they should not exist.

I hope this helps.


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TempBatch]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TempBatch]
GO

CREATE TABLE [dbo].[TempBatch] (
[batchnum] [int] NOT NULL ,
[seq] [int] NOT NULL ,
[ssn] [varchar] (11) NOT NULL ,
[enr_type_code] [int] NOT NULL ,
[check_num] [varchar] (10) NULL ,
[bank_name] [varchar] (30) NULL ,
[check_amt] [money] NULL ,
[cc_num] [varchar] (30) NULL ,
[exp_date] [varchar] (5) NULL ,
[date_verified] [datetime] NULL ,
[app_flag] [varchar] (1) NULL ,
[app_code] [varchar] (10) NULL ,
[cc_type_code] [int] NOT NULL ,
[lsn_id] [int] NOT NULL
) ON [PRIMARY]
GO


SET NOCOUNT ON
DECLARE @table_name varchar(40)
DECLARE tables_cursor CURSOR FOR
select name from sysobjects
where type = 'U' and name like 'CCC%'
order by name

OPEN tables_cursor

-- Perform the first fetch.
FETCH NEXT FROM tables_cursor INTO @table_name

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

EXEC('Insert TempBatch (batchnum,seq,ssn,enr_type_code,check_num, bank_name,check_amt,cc_num,exp_date,date_verified,app_flag,app_code,cc_type_code,lsn_id) Select batchnum,seq,ssn,enr_type_code,check_num, bank_name,check_amt,cc_num,exp_date,date_verified,app_flag,app_code,cc_type_code,lsn_id FROM ' + @table_name)

FETCH NEXT FROM tables_cursor INTO @table_name

END

CLOSE tables_cursor
DEALLOCATE tables_cursor
GO

Reply With Quote
  #3  
Old July 9th, 2008, 12:57 PM
sachincp sachincp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 1 sachincp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 25 sec
Reputation Power: 0
Thumbs up Thanks a lot

Hello rongower
u r idea really helped me a lot .
Once again thanks a lot .

Sachin

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > CANT USE A VARIABLE INSTEAD OF TABLE NAME (eg From @var)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway