| Author |
Topic |
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-18 : 07:02:59
|
| Hi..when i am trying to run this script its throwing Incorrect syntax near 'sq'.this is my scriptDECLARE @TableName sysnameDECLARE @SQL nvarchar(max) DECLARE tables_cursor CURSOR FAST_FORWARDFORSELECT name FROM sys.tablesOPEN tables_cursorFETCH NEXT FROM tables_cursor INTO @TableName WHILE @@FETCH_STATUS = 0BEGIN SELECT @SQL = 'Insert into RECORD_COUNT values('sq.table name','sq.rows_cont) from ('(SELECT ''' + @TableName + '''as TABLE_NAME, COUNT(*) as ROWS_COUNT'+'FROM ['+ @TableName+'])sq' EXEC SP_EXECUTESQL @SQL FETCH NEXT FROM tables_cursor INTO @TableNameEND CLOSE tables_cursorDEALLOCATE tables_cursorthanxyashu |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-18 : 07:07:11
|
Oh boy.Can you please give an example how your statement inside @SQL should look? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-18 : 07:13:09
|
| Hi..Fred,i am looking to get the 2 columns in the record_count tablewhich insludes all the sys.tables.. table_names and rows as row_count.thanxyashu |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-18 : 07:15:24
|
Madhivanan,i have to do this for few more tables and finally want to join all the tables into 1 so..for that reason i am using cursor.thanxquote: Originally posted by madhivanan Haven't you seen my reply here?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=141523Why are you using a cursor?MadhivananFailing to plan is Planning to fail
yashu |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-18 : 07:18:17
|
| See what you get hereInsert into RECORD_COUNT (table name,rows_cont) select object_name(id),rows from sysindexes where object_name(id)='table_name' and indid<2SELECT * FROM RECORD_COUNTMadhivananFailing to plan is Planning to fail |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-18 : 07:31:57
|
| Madhivanan,this is the script which i early used and i am happy with this (SELECT tab.name AS TABLE_NAME,ind.rows as RECORD_COUNT INTO RECORD_COUNTFROM sys.tables tab INNER JOINsys.sysindexes ind ON tab.object_id = ind.id AND ind.indid < 2);what i am trying is i want to create 3 tables 1)RECORD_COUNT this includes Table_Names and Row_count all the sys.tables.2)INSERT_DATE_COUNT this includes table_names and count for INSERT_DATE>= getdate(),this insert date column originally staying in that perticular table ex: there 100 table in Table_Names column and i want to get no_of_records inserted to that individual table >=getdate() based on that insert_date column .insert_date column exists for all the tables not considering this insert date from sys.tables, i want this INSERT_DATEfrom original data from the tables.3)UPDATE_DATE_COUNT i have to repeat the same for this aswell.finall i want to write SP whih will get all the columns in one tableTABLE_NAME,ROW_COUNT,NO_OF_INSERTS and NO_OF_UPDATESI am writing the below script for getting the above resultill add the develop this lateron but iam getting that syntax error at 'sq' when executing upto wat i have written.DECLARE @TableName sysnameDECLARE @SQL nvarchar(max) DECLARE tables_cursor CURSOR FAST_FORWARDFORSELECT name FROM sys.tablesOPEN tables_cursorFETCH NEXT FROM tables_cursor INTO @TableNameWHILE @@FETCH_STATUS = 0BEGINSELECT @SQL ='Insert into RECORD_COUNT values('sq.table name','sq.rows_cont) from ('(SELECT ''' + @TableName + '''as TABLE_NAME, COUNT(*) as ROWS_COUNT'+'FROM ['+ @TableName+'])sq'EXEC SP_EXECUTESQL @SQL FETCH NEXT FROM tables_cursor INTO @TableNameEND CLOSE tables_cursorDEALLOCATE tables_cursoryashu |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-30 : 03:40:59
|
| Print @SQL and post the resultMadhivananFailing to plan is Planning to fail |
 |
|
|
satya068
Posting Yak Master
233 Posts |
Posted - 2010-03-30 : 04:14:38
|
| hello madhavan,this problem was already solved..no i am facing with different issue whic i mentioned in new topic .thanx for ur help..Satya |
 |
|
|
|