I have Googled this without any success.
Here is the SQL query and I wish to turn it into LINQ2SQL.
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID WHERE t.Name = 'Address'
At first I tried accessing just the sys.table like this:
from n in _db.sys.table where n.table select n
_db = new DBDataContext(ConfigurationManager.ConnectionStrings["DATABASE"].ConnectionString);
I am however receiving the following error - Cannot resolve symbol 'sys'
Can you even do this in LINQ2SQL?
Thanks in advance.
Clare :-)
You could also develop a View to access sys.tables, and map that View...
A stored proc can work well too - you just drag it in and it creates a new type with all the right in and out parameters.
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
I have a large number of SQL 2008 R2 views. I would like to know which database fields are referenced in the views.
Is there a way that I can query the schema to list out these column names?
Use this query against sys.sql_dependencies.
SELECT
ViewName = O.name,
ReferencedTableName = X.name,
ReferencedColumnName = C.name,
T.is_selected,
T.is_updated,
T.is_select_all,
ColumnType = M.name,
M.max_length,
M.precision,
M.scale
FROM
sys.sql_dependencies AS T
INNER JOIN sys.objects AS O ON T.object_id = O.object_id
INNER JOIN sys.objects AS X ON T.referenced_major_id = X.object_id
INNER JOIN sys.columns AS C ON
C.object_id = X.object_id AND
C.column_id = T.referenced_minor_id
INNER JOIN sys.types AS M ON
M.system_type_id = C.system_type_id AND
M.user_type_id = C.user_type_id
WHERE
O.type = 'V'
ORDER BY
O.name,
X.name,
C.name
You can look at the view definition and see the referenced tables there. For that you can simply use sp_helptext like so:
sp_helptext 'vStores'
You can retrieve the view defintion from the metatdata in 'sys.objects', see also this answer:
Is there a way to retrieve the view definition from a SQL Server using plain ADO?
That won't give you the underlying tables directly, though.
SQL Server does this trick itself when you define an object as having Schema Binding: when any objects are changed that are referenced by other objects using schema binding, that change is stopped and an error is given. Perhaps you can look into how SQL Server keeps track of those references to see what columns are used in a view. More on Schema binding here: https://www.mssqltips.com/sqlservertip/4673/benefits-of-schemabinding-in-sql-server/
I'm using a piece of code (via Excel VBA) which seems to be in several spots around the internet. The intention is to find all tables which contain a certain field/column.
This is my SQL string:
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE 'MyCol'
ORDER BY schema_name, table_name;
This works perfectly for most tables, but not views. Is there a way to look within views as well?
If anyone has any ideas, I'd really appreciate feedback. I hope this all makes sense as I am fairly new to SQL, but I have been doing VBA for a long time. Thanks so much in advance!
Use sysobjects instead of systables
sysobjects is most DML based objects (except triggers) whereas systables is just well... tables
http://technet.microsoft.com/en-us/library/ms190324.aspx
and becareful of sys.objects vs sysobjects.
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/a965676e-d4d9-4365-ad0a-58ca26ec4701/differenece-between-sysobjects-and-sysobjects-in-sql-server-2005-?forum=sqlkjmanageability
With C# and SQL Server 2005 and by using DbConnection.GetSchema() method, I want to get all a table's columns (not of views) only. I have found two collection names related to this
Columns that returns table and views' columns
ViewColumns returns all the view's columns
Neither of above two returns table columns only, nor they have any property to filter Table-columns.
Any help is respected.
I don't see any easy way to do this with this particular API you're trying to achieve this with - but why not just use a query like this to get your information?
SELECT
c.name AS 'ColumName',
ty.Name AS 'TypeName',
c.max_length,
c.is_identity,
c.is_nullable,
t.name AS 'TableName'
FROM sys.columns c
INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
INNER JOIN sys.tables t ON c.object_id = t.object_id
Just load that into a SqlCommand and execute it against the open connection you have and read the result into some DataTable or other structure for your use. This gives you only table columns - and all of them.
When I run this script to search particular text in sys.columns and I get a lot of "dbo.syncobj_0x3934443438443332" like rows.
SELECT c.name, s.name + '.' + o.name
FROM sys.columns c
INNER JOIN sys.objects o ON c.object_id=o.object_id
INNER JOIN sys.schemas s ON o.schema_id=s.schema_id
WHERE c.name LIKE '%text%'
If I get it right, they are replication objects. Is it so? Can i just throw them away from my query just like o.name NOT LIKE '%syncobj%' or there's another way?
Thank you.
I've found a solution. Doesn't know, if it's the best one or not.
SELECT c.name, s.name + '.' + o.name
FROM sys.columns c
INNER JOIN sys.objects o ON c.object_id=o.object_id
INNER JOIN sys.schemas s ON o.schema_id=s.schema_id
WHERE c.name LIKE '%text%' AND o.type = 'U'
The result is fine now. As I said syncobj's are replication objects and they don't have a meaning for us. They're used for replication purposes only.
http://www.developmentnow.com/g/114_2007_12_0_0_443938/syncobj-views.htm
EDIT:
Forgot to add, syncobj's are stored in DB as Views, so if you need list of views, you'll probably need to ignore them as I did in my question.
While checking difference between syncobj's and my views, the only difference is is_ms_shipped column. For syncobj it's 1, for others 0. It means that syncobj views are created by system.
P.S. I'll wait for some time and if nobody gives another answer, I'll accept mine.
When you create a replication that does not include all the fields or other meta data changes from the original table. If you do a generate script from a publication it will show you how it is created (see below). The view provide a object to generate the bcp extracts during the initial snapshots.
Here is an example
-- Adding the article synchronization object exec sp_articleview #publication = N'publication_data', #article = N'tablename',
#view_name = N'syncobj_0x4239373642443436', #filter_clause = N'',
#force_invalidate_snapshot = 1, #force_reinit_subscription = 1 GO
P.S. I recently had a problem when the I dropped replication, it failed to drop these and then you have to manually drop the system views to reuse a replication script. Giving a error message
Msg 2714, Level 16, State 3: There is already an object named
'syncobj_0x3437324238353830' in the database.
Which caused the bcp to fail during the snapshot.