| Author |
Topic |
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2011-05-24 : 03:58:56
|
| hi,i have a lot of tables , and i want to clean all the characters in all the tables.is the function (that i give the name of DB and it clean all tables in it)? |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-24 : 04:19:54
|
| I suspect not.What do you mean by clean?It sounds like you want to convert data.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2011-05-24 : 04:42:20
|
| suppose in one of rows(i column of table) , i have this string'tes^@&t %stri)-n!g' i want to get test stringi want create function that over all clumns in all tables in DBand replace(clean all unwanted characters ) |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-24 : 07:15:34
|
| You can eaily create a function to work on a column - but it would mean passing individual strings - i.e. running for each column and it would act on a row by row basis - i.e. a function call for each column and row.Here is some code to do it - just change the patindex values.You might also consider getting a list of rows with offending data - get the first unwanted character - replacing that in the column then looping until there are none left.It depends on how much data and how many coumns/rows have data to change and how long you have to do it.Of course it would be better to do it when thedata is inserted.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2011-05-24 : 07:32:44
|
how i running for each column ,how can i get then columns each timeDECLARE @table_name VARCHAR(50)DECLARE tableName_Cursor CURSOR FOR SELECT table_name FROM information_schema.tablesWHERE table_type='Base table' AND table_name<>'clean_chars'OPEN tableName_CursorFETCH NEXT FROM tableName_Cursor INTO @table_nameWHILE @@fetch_status=0BEGIN...............................FETCH NEXT FROM tableName_Cursor INTO @table_nameENDCLOSE tableName_CursorDEALLOCATE tableName_Cursor |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-24 : 08:08:43
|
| Run this for a table then run the output (after creating your function)with cte as(select Column_Namefrom INFORMATION_SCHEMA.COLUMNSwhere TABLE_NAME = 'mytbl'and DATA_TYPE like '%CHAR%')select 'update mytbl set ' + stuff ( ( select ',[' + Column_Name + '] = dbo.myfunc(' + Column_Name + ')' from cte for xml path('') ) ,1,1,'')==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2011-05-24 : 08:11:23
|
| how i execute the output? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-24 : 08:26:49
|
| In a query window?How do you execute sql at the emoment?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2011-05-24 : 09:15:00
|
i run it and he wrote to me :"Msg 319, Level 15, State 1, Line 5Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon."this query: DECLARE @str varchar(max)SET @str= '' with cte as(select Column_Namefrom INFORMATION_SCHEMA.COLUMNSwhere TABLE_NAME = 'tablea'and DATA_TYPE like '%CHAR%')select @str= @str +'update tableA set ' + stuff ((select ',[' + Column_Name + '] = dbo.clean_data(' + Column_Name + ')'from ctefor xml path('')),1,1,'') EXEC @str |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-24 : 10:19:06
|
| Exactly what it says. add a semi-colon before the with;with cte as==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2011-05-24 : 12:40:43
|
thanks,can you help me with this error Msg 203, Level 16, State 2, Line 4The name 'update tableA set [ColumnA] = dbo.clean_data(ColumnA),[ColumnB] = dbo.clean_data(ColumnB)' is not a valid identifier even i run that i get same error:DECLARE @str varchar(max)SET @str= 'update tableA set [ColumnA] = dbo.clean_data(ColumnA),[ColumnB] = dbo.clean_data(ColumnB)';EXEC @str |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-05-24 : 13:41:47
|
| change to EXEC (@str) with the brackets. |
 |
|
|
|