I would like to make a query or procedure that would look for all the fields with a specific name in a database and if the size of the field was less than N , it would establish a new value. That is, set all named fields articulo
of a database Varchar(8)
to Varchar(15)
. Can someone guide me on how to do it? Thank you very much.
It's MSSQL Server, sorry for not specifying it, I thought Transact-sql in the tags was enough, sorry.
Here you have a basic proposal, which serves as a solution to what you ask for and that you can modify as appropriate or put in a procedure passing parameters.
We create a cursor that through the system tables
sys.objects
,sys.columns
,sys.schemas
andsys.types
andWHERE
return us the list of matching fields and their properties (schema, table name, field name and type). In this case only the columns whose name is 'article'.For each of them, it mounts a string with the statement
ALTER
and executes it. Each of them would be of the type:It only takes user tables (
where a.type='u'
). It only takes fields of typevarchar
ynvarchar
, whose length is 8, and transforms it to length 15.EDIT When a field associated with a Constraint is found, it will give an error. This Constraint can be from the same table or from another table that refers to the one being modified. Therefore you can temporarily disable these restrictions by
then turn them back on