What is syncobj in SQL Server - sql

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.

Related

Getting column information from system MS SQL Server

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

Ad hoc Updates to System Catalogs

I know this question has been addressed elsewhere, but I cannot figure this out.
I have this view:
SELECT t.name AS 'TableName'
,c.name AS 'ColumnName'
,SEP.VALUE as 'DETAILS'
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
left join sys.extended_properties sep on t.object_id = sep.major_id
and c.column_id = sep.minor_id
and sep.name = 'MS_Description'
And when I run this query, I get the expected results:
SELECT * FROM [VIEW] WHERE DETAILS IS NOT NULL
However, There was a description I added to one of the columns through the table designer that I later removed. My issue is that this row still returns with my query, with DETAILS displayed as an empty string. I would like for this row to no longer show up, so I ran:
UPDATE sys.extended_properties SET VALUE=null WHERE major_id=1781581385 AND minor_id=4
To which I get the following error:
Msg 259, Level 16, State 1, Line 12
Ad hoc updates to system catalogs are not allowed.
I have tried Setting the Database to single user, as suggested by Martin1 here:
https://ask.sqlservercentral.com/questions/40909/ad-hoc-update-to-system-catalogs-is-not-supported.html
I feel I am missing something simple, and would appreciate any feedback. Again, I apologize for the duplicate thread.

How to search for a specific string occurrence in stored procedures

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

Temp tables on the current connection

if I do:
select * from tempdb.sys.tables
I will see all the temporary tables in the system, however that view does not have information about which connection/user each table belongs to. I'm interested in finding only the tables I've created on my current connection. Is there a way to do this?
thanks - e
p.s. yes, I could try reading each table listed with the notion that those that succeed should prove to be mine (on recent versions one can't read other connections' tables) but that is too costly an approach since there may be thousands of tables on the system
p.p.s. I did read Is there a way to get a list of all current temporary tables in SQL Server? which asks the right question but did not get a good answer
Assuming you don't name your #temp tables with three consecutive underscores, this should only pick up your #temp tables. It won't, however, pick up your table variables, nor can you change this code somehow to pick the tables on someone else's connection - this only works because OBJECT_ID('tempdb..#foo') can only return true for a table in your session.
SELECT
name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1),
t.[object_id]
FROM tempdb.sys.tables AS t
WHERE t.name LIKE '#%[_][_][_]%'
AND t.[object_id] =
OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));
You might also be interested in space used by each of these tables (at least for the heap or clustered index), e.g.:
SELECT
name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1),
t.[object_id],
p.used_page_count,
p.row_count
FROM tempdb.sys.tables AS t
INNER JOIN tempdb.sys.dm_db_partition_stats AS p
ON t.[object_id] = p.[object_id]
WHERE t.name LIKE '#%[_][_][_]%'
AND p.index_id IN (0,1)
AND t.[object_id] =
OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));
You could extend that to show total space for all indexes. I didn't bother aggregating per partition since these are #temp tables.
select *
from tempdb.sys.objects
where object_id('tempdb.dbo.' + name, 'U') is not null
AND name LIKE '#%'
Would tell you all the tables in tempdb beginning with # that you can access, but Aaron's script just blew me out of the water haha
To find out the name of the user who create the object you just need to check for the schema ID and cross reference with the Schemas table
Select sch.name as 'User Owner' from tempdb.sys.tables TBL
join tempdb.sys.schemas SCH on TBL.schema_id = SCH.schema_id
where TBL.name like '#tmp_Foo%'

use tsql to detect XML dependency

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'