| Author |
Topic |
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-25 : 12:03:09
|
| Hi..could you pls tell where i have done wrongSELECT RDB_PAS_APPOINTMENTS AS TABLE_NAME,Sum(case when(getdate() >= CONVERT(datetime,'25/03/2010 00:00:00') then 1 else 0 end) as INSERT_COUNT FROM RDB_PAS_APPOINTMENTS group by RDB_PAS_APPOINTMENTS error message isMsg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'then'.thanxSatya |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 12:07:19
|
quote: Originally posted by satya068 Hi..could you pls tell where i have done wrongSELECT RDB_PAS_APPOINTMENTS AS TABLE_NAME,Sum(case when(getdate() >= CONVERT(datetime,'25/03/2010 00:00:00') then 1 else 0 end) as INSERT_COUNT FROM RDB_PAS_APPOINTMENTS group by RDB_PAS_APPOINTMENTS error message isMsg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'then'.thanxSatya
remove the unnecessary (------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-25 : 12:09:52
|
| thanx visakhit worked.Satya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 12:20:12
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-25 : 12:30:13
|
| Hi..there is another error whis is invalid column could you plz tell me!Msg 207, Level 16, State 1, Line 1Invalid column name 'RDB_PAS_APPOINTMENTS'.Msg 207, Level 16, State 1, Line 1Invalid column name 'RDB_PAS_APPOINTMENTS'. SELECT RDB_PAS_APPOINTMENTS AS TABLE_NAME,Sum(case when getdate() >= CONVERT(datetime,'25/03/2010 00:00:00') then 1 else 0 end) as INSERT_COUNT FROM RDB_PAS_CLINICS group by RDB_PAS_APPOINTMENTSthanxSatya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 12:32:32
|
| do you have column RDB_PAS_APPOINTMENTS in RDB_PAS_CLINICS table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-25 : 12:34:08
|
| SORRY VISAHKTHIS THE ACTUAL SCRIPT SELECT RDB_PAS_APPOINTMENTS AS TABLE_NAME,Sum(case when getdate() >= CONVERT(datetime,'25/03/2010 00:00:00') then 1 else 0 end) as INSERT_COUNT FROM RDB_pas_APPOINTMENTS group by RDB_PAS_APPOINTMENTSMsg 207, Level 16, State 1, Line 1Invalid column name 'RDB_PAS_APPOINTMENTS'.Msg 207, Level 16, State 1, Line 1Invalid column name 'RDB_PAS_APPOINTMENTS'.Satya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 12:37:17
|
| what you've column and table names same?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 12:37:49
|
| whats your table and whats the column you want to group on?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-03-25 : 12:41:13
|
Another thing you've done wrong, you've laid your code out so poorly, that relatively simple syntax errors are more difficult to spot. Do yourself a favour, and take the time to lay your code out neatly. Something like this:SELECT RDB_PAS_APPOINTMENTS AS TABLE_NAME, Sum(case when getdate() >= CONVERT(datetime,'25/03/2010 00:00:00') then 1 else 0 end) as INSERT_COUNT FROM RDB_PAS_APPOINTMENTS GROUP BY RDB_PAS_APPOINTMENTS It makes your code easier to read, and therefore, syntax errors easier to spot. Also, you don't need to convert the date string to a DATETIME type.There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-25 : 12:41:28
|
| COLIMN NAME IS INSERT_DATETABLE NAME IS RDB_PAS_APPOINTMENTSI NEED COUNT FOR THE INSERT_DATE COLUMN FROM THE RDB_PAS_APPOINTMENTSINSERT_DATE>=SYSDATE()WANT COUNT FOR INSERT_DATE FOR KNOWING no of records inserted today.thanksSatya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 12:45:26
|
then this is enoughSELECT count(*) as INSERT_COUNT FROM RDB_pas_APPOINTMENTS WHERE INSERT_DATE >= DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)AND INSERT_DATE < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-25 : 12:55:29
|
| Visakh,,it worked.thanx for ur great help.Satya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 13:00:30
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-25 : 13:01:54
|
| Visakh..when i used seperately the data is abe to count from RDB table,why its not reading when i used this script in the stored procedure.its only counting RECORD_COUNT then its stopping its not procedding to INSERT_COUNT and UPDATE_COUNTTHIS IS MY SCRIPTuse PAS_RDBDECLARE @TableName sysname, @SQL nvarchar(max), @INSERT_DATE datetime, @UPDATE_DATE datetime, @INSERT_COUNT int, @UPDATE_COUNT intDECLARE @TODAY VARCHAR(10)SET @TODAY = CONVERT(varchar(10), getdate(), 121)DECLARE @DATABASE varchar(255)TRUNCATE TABLE RECORD_COUNTTRUNCATE TABLE INSERT_COUNTTRUNCATE TABLE UPDATE_COUNTDECLARE table_cursor CURSOR FAST_FORWARDFORSELECT name FROM sys.tablesOPEN table_cursorFETCH NEXT FROM table_cursor INTO @TableName WHILE @@FETCH_STATUS = 0BEGIN SELECT @SQL = 'Insert into RECORD_COUNT (TABLE_NAME, ROW_COUNT) SELECT ''' + @TableName + '''as TABLE_NAME, COUNT(*)as ROWS_COUNT ' +'FROM ['+ @TableName+']' print @sql EXEC SP_EXECUTESQL @SQL SELECT @SQL ='insert into INSERT_COUNT (TABLE_NAME, NO_OF_INSERTS)SELECT ''' + @TableName + '''as TABLE_NAME,Sum(case when(' + convert(varchar, @INSERT_DATE )+ ' >= convert(varchar, @TODAY) then 1 else 0 end) as INSERT_COUNT' +'FROM [' + @TableName + ']''group by ' + @tablename + '' print @sql EXEC SP_EXECUTESQL @SQL SELECT @SQL ='insert into UPDATE_COUNT (TABLE_NAME, NO_OF_INSERTS)SELECT ''' + @TableName + '''as TABLE_NAME,Sum(case when(' + convert(varchar,@UPDATE_DATE ) + ' >= convert(varchar, @TODAY ) then 1 else 0 end) as INSERT_COUNT' +'FROM [' + @TableName + ']''group by ' + @tablename + ''print @sql EXEC SP_EXECUTESQL @SQL FETCH NEXT FROM table_cursor INTO @TableNameENDCLOSE table_cursorDEALLOCATE table_cursorIF (Object_ID(N'TOTAL_RECORD_COUNT')IS NOT NULL) DROP TABLE TOTAL_RECORD_COUNTgo SELECT RECORD_COUNT.TABLE_NAME,RECORD_COUNT.ROW_COUNT,INSERT_COUNT.NO_OF_INSERTS,UPDATE_COUNT.NO_OF_UPDATESINTO TOTAL_RECORD_COUNTfromRECORD_COUNT left outer join INSERT_COUNT on RECORD_COUNT.TABLE_NAME = INSERT_COUNT.TABLE_NAMEleft outer join UPDATE_COUNT on RECORD_COUNT.TABLE_NAME = UPDATE_COUNT.TABLE_NAMESatya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 13:14:01
|
| may be you dont have matching records in them and left join is returning NULL?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|