i have a XML schema bound to a table. however, sometimes testers piggyback and bind to this schema too. when there is this "ninja" XML table reference, any alteration to this schema is painful.
i'd like to run a query before schema altering and raise exception if the XML schema is bound to more than one table. i've looked at sys.sql_dependencies and few of the other sys.xml_XXXX tables, but it's not clear how to do this in tsql. is something like this possible?
Something like this might be helpful
select object_name(object_id) as TableName,
col_name(object_id, column_id) as ColumnName
from sys.column_xml_schema_collection_usages as U
inner join sys.xml_schema_collections as S
on U.xml_collection_id = S.xml_collection_id
where S.name = 'YourXMLSchemaCollectionName'
This one is to find where the schema is used in a XML parameter.
select object_name(object_id)
from sys.parameter_xml_schema_collection_usages as P
inner join sys.xml_schema_collections as S
on P.xml_collection_id = S.xml_collection_id
where S.name = 'YourXMLSchemaCollectionName'
Related
When I am using the below SQL statement to retrieve the column information of a SQL Server database. I am getting more columns than there actually are I presume this is because of the system columns that are there also.
SELECT
c.name Field,
t.name Type,
c.Precision,
c.Scale,
c.max_length,
c.is_nullable,
c.collation_name
FROM
sys.columns c
INNER JOIN
sys.types t ON t.system_type_id = c.system_type_id
WHERE
object_id = OBJECT_ID('SOPOrders')
You will see the above query is producing ten Order Memos when in fact their should only be the one the var char I still want to be able to report back the prevision dataype maxlength and the null able factor so what is wrong with the above query please.
There may be a couple of reasons for that. First of all, you are joining types by wrong condition - you should use user_type_id instead of system_type_id on both sides. The latter gives you the underlying built-in type which was used as a basis of a user-defined one. See this query, for example:
select * from sys.types t where t.user_type_id != t.system_type_id;
Another possible thing is that the table has a sparse column set, but I might be wrong here.
And, of course, make sure you are actually querying the information about the right table - always include the schema name qualifier, along with the object name, such as:
WHERE object_id=object_id('dbo.SOPOrders')
Failure to do so will not result in duplication you observe, but following it will save you a lot of time trying to figure out the cause of intermittent inconsistencies, when you will have objects with the same name in different schemas.
Why not using Information_Schema.COLUMNS instead??
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'SOPOrders'
Or try to join on
ON c.user_type_id = t.user_type_id
instead of
ON t.system_type_id=c.system_type_id
Provided there are tables Application, Person, Status, etc...
Status and Person have a link to Application.
Is there a simple sql query that may be written that will give a list of all the tables linked to the Application table (in contrast of using the database diagram).
Application expected results:
Status
Person
Please advise on a feasible solution (if any).
Using SP_depends. There are other ways as well like DMV's.
sp_depends 'TableName'
Assuming you're using MSSQL Server then you can run
EXEC sp_fkeys 'YourTableName'
Otherwise you can use
SELECT *
FROM information_schema.referential_constraints
WHERE table_name = <tablename>
to establish the foreign key relationships
Another way to check each table name that references to other tables would be this:
SELECT DISTINCT OBJECT_NAME(f.parent_object_id) AS TableName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o
ON o.OBJECT_ID = fc.referenced_object_id
You can, of course, add more conditions to check for a single table, for example:
WHERE OBJECT_NAME(f.referenced_object_id) = 'Application'
This should give you a result like:
TableName ReferenceTableName
---------- ------------------
Status Application
Person Application
Using SQL Server.
I have a very extensive query, with a lot of aliasing, etc...
Is there a way, using just SQL (stored proc is fine, but not PHP, etc), to get a list of all column names from this query? (I realize I will have to probably embed my query inside of this solution but that is fine. Just a temporary measure.)
Thanks!
If you're using SQL Server 2012 or later you can take advantage of sys.dm_exec_describe_first_result_set
SELECT name
FROM
sys.dm_exec_describe_first_result_set
('Your Query Here', NULL, 0) ;
DEMO
There are various ways that you can get the columns out of the query, such as:
select top 0 s.*
from (<your query here>) s;
Then you can parse the results.
However, I have found another approach useful. Create either a view or a table using the same logic:
select top 0 s.*
into _TempTableForColumns
from (<your query here>) s;
Then use information_schema (or the system tables if you prefer):
select *
from information_schema.columns
where table_name = '_TempTableForColumns' and schema_name = 'dbo';
drop table _TempTableForColumns;
The advantage of this approach is that you can get type information along with the column names. But the engine still has to run the query and that might take time even though no rows are returned. Although the column names and types are available after compiling, I am not aware of a way to get them without also executing the query.
After SQL Server 2008
select *
from sys.columns c
inner join sys.objects o on c.object_id = o.object_id
where o.name = 'TableName'
Before
select *
from syscolumns c
inner join sysobjects o on c.id = o.id
where o.name = 'TableName'
I have a query that selects particular stored procedures, triggers and functions in my SQL database. I have retrieved 86 in total.
I need to find which of these are using a certain string inside.
This string is a name of a table.
I do not want to open each of these individually and search for it.
I'm thinking of having a script that looks inside of these and find what I need.
What I think I might need is something like from tableName or inner join on tableName or join on tableName and etc
What is a good way to do it in SQL
Thank you
To search one SQL Server database for a specific piece of text in a stored procedure you could do this:
DECLARE #SearchText VARCHAR(100) = 'TableXYZ';
SELECT DISTINCT
o.name,
o.type_desc
FROM
sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id = o.object_id
WHERE
m.[definition] Like '%' + #SearchText + '%';
Obviously this is a bit of a hack and won't work out if your table name is called something daft like "SELECT" as that will just cause loads of false-positive results.
One alternative is to use the sys.dm_sql_referencing_entities system table, but my experience with this is that it can't be trusted. In general dependencies don't work very well with SQL Server due to some poor design decisions, e.g. deferred name resolution.
Microsoft SQL would cover Access, Sybase and SQL Server (and possibly more) .... But I suspect you are using SQL Server, as such I would suggest sys.dm_sql_referencing_entities rather than searching a definition for a particular string -
SELECT r.* , o.type_desc, m.definition
FROM sys.dm_sql_referencing_entities('dbo.TableName', 'OBJECT') AS r
INNER JOIN sys.all_objects AS o
ON o.[object_id] = r.referencing_id
INNER JOIN sys.sql_modules AS m
ON m.[object_id] = o.[object_id];
Here is a simple and easy to use Sql query
SELECT name
FROM sys.procedures
WHERE Object_definition(object_id) LIKE '%strHell%'
And here's a link for more info
Given a column name how can I find
which tables in database contain that
column ?
or alternatively
How can I find that particular column
exists for all tables in Database ?
Note: Kindly explain answers with Examples as that I get most knowledge from the answer.
Edit: I am using MySQL Database.
SELECT * FROM information_schema.columns WHERE COLUMN_NAME = 'mycolumn'
Depends on the database you are using. Many database systems expose a set of tables of views that contain details of the schema. For example, you can get schema information from the SYSTABLE and SYSCOLUMN views in Sybase ASA.
in SQL Server:
select distinct t.name
from sys.Columns c
inner join sys.tables t on c.object_id = t.object_id
where c.name = 'YOUR_COLUMNNAME'