Locating BLOBs inside of a SQL Server database - sql-server-2008

I have a SQL Server 2008-R2 database that I am migrating it's data to a different SQL Server 2008-R2 database. The database contain over 400 tables but I need to find all tables that contain images and documents that are saved in BLOB format and put those items into a separate database (also SQL 2008-R2).
I have used the following script to locate all tables that have either a Binary, Image, Varbinary, Varchar(MAX), nVarchar(MAX):
SELECT
OBJECT_NAME(c.id) as TableName,
c.name as ColumnName,
c.colid As ColumnOrder,
st.name as UserType,
bt.name as BaseType
FROM
dbo.syscolumns c
INNER JOIN dbo.systypes st ON st.xusertype = c.xusertype
INNER JOIN dbo.systypes bt ON bt.xusertype = c.xtype
WHERE
OBJECTPROPERTY(c.id,'ISTABLE') = 1
AND((st.name = 'nvarchar' AND c.length = 8000) OR
(st.name = 'varchar' AND c.length = 8000) OR
(st.name in('sql_variant','varbinary','binary','image')))
ORDER BY
OBJECT_NAME(c.id),
c.colid
But this only cuts the list in half, other than building SELECT Statements for each of these tables is there an easier way of finding BLOB files in this database?

Related

Getting list of all SQL users from multiple servers

I have an task of getting the user reconciliation where we have around 50 SQL servers, I need to take the list of database users from all the SQL servers in one place and mail it to team. Any ideas will be helpful and I have SCOM server also.
I don't know any quick or fancy way, but you could do:
;with ServerPermsAndRoles as
(
select
spr.name as principal_name,
spr.type_desc as principal_type,
spm.permission_name collate SQL_Latin1_General_CP1_CI_AS as security_entity,
'permission' as security_type,
spm.state_desc
from sys.server_principals spr
inner join sys.server_permissions spm
on spr.principal_id = spm.grantee_principal_id
where spr.type in ('s', 'u')
union all
select
sp.name as principal_name,
sp.type_desc as principal_type,
spr.name as security_entity,
'role membership' as security_type,
null as state_desc
from sys.server_principals sp
inner join sys.server_role_members srm
on sp.principal_id = srm.member_principal_id
inner join sys.server_principals spr
on srm.role_principal_id = spr.principal_id
where sp.type in ('s', 'u')
)
select *
from ServerPermsAndRoles
order by principal_name
https://msdn.microsoft.com/en-us/library/ms187328.aspx
On each of the servers, and stuff the results in a single Excel spreadsheet - then using Excel, filter only unique entries.

Lookup two Display Names using the same Table on 1 unique assignment in SQL Tables

I am trying to write a query where two different UIDs need to lookup a Resource Name for both, but separately.
In other words, for each Task, there are resources assigned and one status manager. This converts in SQl to an Assignment, unique to a resource, but with the same status manager. However, no where in the database can one see the Status Manager's Name on a given assignment.
The assignment does have "TaskStatusManagerUID" available. The name of the Status Manager can be determined by tying it back to MSP_EPMResource table where TaskStatusManagerUID = ResourceUID.
The catch is, for my report, I need to be able to look at the ResourceUID and TaskstatusManagerUID and determine the names of each on the same assignment.
While I have been successful with a join to display the name for one or the other, I have not been able to determine how to show the name for both the Resource and TaskStatusManager.
This is an example of what I am trying to display (parentheses added for readability):
(AssignmentUID) (Task Name) (Resource Name) (Task Status Manager Name)
See more info below:
This is the code I have been working with, but have been unsuccessful:
Select top 100
c.[assignmentuid],
a.[taskname],
c.[resourceuid],
b.[resourcename],
a.[taskstatusmanageruid],
d.[StatusManager]
from [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmAssignment] c
join [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmTask_UserView] a
on a.[TaskUID] = c.[TaskUID]
join [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmResource] b
on b.[ResourceUID] = c.[ResourceUID]
join (select b.resourcename StatusManager
from [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmResource] b) d
on d.[StatusManager] = a.[taskstatusmanageruid]
group by
c.[assignmentuid],
a.[taskname],
c.[resourceuid],
b.[resourcename],
a.[taskstatusmanageruid],
d.[StatusManager]
Currently, I am getting "Conversion failed when converting from a character string to uniqueidentifier."
On your joins you have on a.[TaskUID] = c.[TaskUID], on b.[ResourceUID] = c.[ResourceUID], and on d.[StatusManager] = a.[taskstatusmanageruid], of which, I am assuming that the last one is causing you the issue. Try instead
join (select b.resourcename StatusManager
from [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmResource] b) d
on d.[StatusManager] = CONVERT(CHAR, a.[taskstatusmanageruid])
This will convert the GUID contained in taskstatusmanageruid to a char string, allowing it to compare successfully.
You could also, instead of converting the value, cast the value CAST(a.[taskstatusmanageruid] AS CHAR
EDIT
Due to the nature of the GUID, you may not be able to convert/cast it to a char value, in which case you would need to convert/cast both fields to either varchar or nvarchar:
join (select b.resourcename StatusManager
from [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmResource] b) d
on CONVERT([N]VARCHAR, d.[StatusManager]) = CONVERT([N]VARCHAR, a.[taskstatusmanageruid])
OR
join (select b.resourcename StatusManager
from [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmResource] b) d
on CAST(d.[StatusManager] AS [N]VARCHAR) = CAST( a.[taskstatusmanageruid] AS [N]VARCHAR)
Thanks to Jeff Beese's extra set of eyes, it was enough for me to get the last piece in place!
Select top 100
c.[assignmentuid],
a.[taskname],
c.[resourceuid],
b.[resourcename],
a.[taskstatusmanageruid],
d.[StatusManager]
from [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmAssignment] c
join [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmTask_UserView] a
on a.[TaskUID] = c.[TaskUID]
join [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmResource] b
on b.[ResourceUID] = c.[ResourceUID]
join (select b.resourcename as StatusManager,
b.ResourceUID
from [PRJPROD_ProjectWebApp].[dbo].[MSP_EpmResource] b) d
on d.[resourceuid] = a.[taskstatusmanageruid]
group by
c.[assignmentuid],
a.[taskname],
c.[resourceuid],
b.[resourcename],
a.[taskstatusmanageruid],
d.[StatusManager]

How to merge two tables into one using mongify

I am planning to migrate my application database from Mysql to Mongo with low schema changes. In my new schema I merged two Mysql tables into one Mongo collection. I want to use mongify (https://github.com/anlek/mongify) gem to populate my existing Mysql data into Mongo with newer schema.
How to do this? Is there a way in mongify to merge two Mysql tables into one?
Mysql tables
user
id
name
nickname
type
user_role
id
role
alias
user_id
I merged the above two tables into single collection in Mongo
user
id
name
type
role
alias
Try Left Join: (Tables will be merged)
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
Export that data to sql format and upload it to mongodb
SELECT country INTO customerCountry
FROM customers
WHERE customerNumber = p_customerNumber;
CASE customerCountry -- Here you have to see if that alias data is set
WHEN 'USA' THEN
SET p_shiping = '2-day Shipping'; -- here you have to write Update Query
ELSE
SET p_shiping = '5-day Shipping';
END CASE;
I think it might help you
Fetch the data using JOIN from MySQL and load that data into MongoDB:
Try this:
SELECT U.id, U.name, U.type, UR.role
FROM USER U
INNER JOIN user_role UR ON U.id = UR.user_id;
An alternative if its difficult to merge two tables ad-hoc then run a mongo shell script post-hoc; they are very easy to program (couple o' lines of js) and simple to execute. plus one can apply any needed conversion, such as into json/bson arrays permitted in mongo but not the mysql.
assuming a user and user_role collection (or table) in mongo.
db.user_role.find({}).forEach(function(doc) {
var roles = doc.roles;
// convert mysql format here if needed.
//var roles_array = JSON.parse(roles) // or whatever?
//var alias = doc.alias;
// if alias is null empty or ??
// dont upsert to avoid orphaned user_role records
db.user.update({"_id": doc.user_id}, {"roles": roles, "alias", doc.alias }, {upsert: false, multi: false});
}
Then execute using the mongo shell
mongo localhost:27017/test myjsfile.js

MySQL using OR in Select Query - when class has been used

Info: Server version: 5.1.39
Php: 5.4
MySQL / phpMyAdmin
Server: Apache
Code is run via: Server SQL Query (copy & paste in the phpMyAdmin) or in MySQL Workbench or using a custom shopping cart manager.
Exports to: Excel (.csv then to .xlsx for sales reports)
Other: I do use a number of tables for referencing
This is part of a larger SELECT Query I am running. For the sake of brevity I have included only the section I currently need looking at.
Select
T5.orders_id As OrdID,
T3.products_name As ProdName,
<continues>
(select value from /*PREFIX*/orders_total T5 where orders_id = T2.orders_id and class = 'ot_reward_points_discount') As ot_reward_points_discount,
(select value from /*PREFIX*/orders_total T5 where orders_id = T2.orders_id and class = 'ot_reward_points') As ot_reward_points_discount,
<more data queries>
Inner Join /*PREFIX*/orders T5 On (T5.orders_id = T2.orders_id)
<number of joins and final WHERE/DESC>
The results end up with me having 2 columns, the data has not merged. I have been directed in to looking at an OR query so that the 2 will result in the one column. There is no concern with results being overwritten as they 1st select & the 2nd select are from different configs in my cart, 1 predates the other.
Because the SELECT chooses from a class value so it returns in column (not row) data per value I have NO idea how to create the OR query so it returns neatly.
Thank you for your help, please let me know if there is further info I need to supply.
Try to use OR this way :
<continues>
(select value from /*PREFIX*/orders_total T5
where orders_id = T2.orders_id and
(class = 'ot_reward_points_discount' or class = 'ot_reward_points')
) As ot_reward_points_discount,
<more data queries>

How to use MSSQL to Row concatenate?

I know this row concatenation question has been asked before but I cannot find answers that will solve my problem. I have an older AS400/DB2 database that I am using an Excel ODBC connector to get to the data. I have used MySQL in the past but this is my firstt attempt at MSSQL. To set up the question I have the following:
A work order number (wono)
A note line number (ntlno1)
A note data 50 character text length (ntda)
For each wono you can have several line numbers (ntlno1) and each line number can have up to 50 characters of data (ntda). I need to be able to concatenate all of the ntda data into one field by work order number and note line number. Here is what I have so far:
SELECT
DISTINCT WOHDR.wono AS WONO, WONOT.ntlno1 AS NoteLineNo, substring((Select ','+WONOT.ntda AS [text()]
From libd09.wopnote0 WONOT
Where WONOT.wono = WOHDR.wono
ORDER BY WONOT.wono
For XML PATH ('')),2, 1000) [wopnote0]
FROM libd09.wophdrs0 WOHDR
LEFT JOIN libd09.wopsegs0 WOSEG ON (WOHDR.wono = WOSEG.wono)
LEFT JOIN libd09.cslusrp0 USR ON (WOSEG.clsdby = USR.uspfid)
LEFT JOIN libd09.woplabr0 WOLBR ON (WOHDR.wono = WOLBR.wono)
LEFT JOIN libd09.wopnote0 WONOT ON (WOHDR.wono = WONOT.wono)
LEFT JOIN libd09.wopmisc0 WOMISC ON (WOHDR.wono = WOMISC.wono)
LEFT JOIN libd09.wopempf0 WOEMP ON (WOMISC.epidno = WOEMP.epidno)
LEFT JOIN libd09.woppart0 WOPART ON (WOHDR.wono = WOPART.wono)
LEFT JOIN tafilev22.employ01 TAEMP ON (WOMISC.epidno = TAEMP.ememp)
WHERE WOHDR.opndt8 BETWEEN ? AND ?
I have only included the pertinent columns though the joins have all the tables that I need. when I run the query I get the following error: Token [ was not valid. Valid tokens: .
I am sure I am missing something simple so any advice or help would be greatly appreciated.