Author |
Topic |
pszivos
Starting Member
10 Posts |
Posted - 2006-10-30 : 03:12:44
|
Hi,I'd need to construnct an EXEC statement in my script and I am pretty much stuck with data conversion. I need to compare a binary variable with a SID value to an actual SID found in ..sysusers. When I construct the EXEC statement the whole line has to be string and I need to find a way to use the binary data as string.So far, I tried to play with CONVERT and CAST, but could not succeed. A simplified version of what I tried to do:declare @t_sid binary(85)select @t_sid = 0x01050000000000051500000043170A324D64492E235F636B 18BA0400000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0000000000000000000000print convert(nvarchar(10), @t_sid)This results a nice square character, nothing else...Any I idea dear experts? |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
pszivos
Starting Member
10 Posts |
Posted - 2006-10-30 : 03:51:29
|
Thank you Peter!I did not indicate, but I need to solve this issue in T-SQL: using SID stored in system tables of databases.Peter SzivosLausanne, Switzerlan |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 03:54:51
|
Then just do a = thingy...T-SQL is able to check for equality between binary value.It would help a little more if you give us more information about what you are trying to accomplish and which values you have.Peter LarssonHelsingborg, Sweden |
 |
|
pszivos
Starting Member
10 Posts |
Posted - 2006-10-30 : 04:47:37
|
OK, I am checking each database if a user (login) is defined or not. As I cannot do a select with a variable (actual database name in it) like select * from @database..sysusers where @database..sysusers.sid = @sid_actual (previously read from MASTER..SYSLOGINS.SID)I am trying to do a big botchwork:EXEC ('USE ['+@db_act+'] SELECT * FROM [' + @db_act + ']..sysusers WHERE [' + @db_act + ']..sysusers.sid = '+ @t_sid <= This is where I need to refer to the binary value as string...Comments:@db_act - name of the actual DB it is checking@t_sid - sid of the login read previously form MASTER..SYSLOGINS.SIDIf the checking is successful, I generate some text file with some commands... |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 04:58:51
|
[code]select * from sysusers where sid = 0x0105000000000005150000007CEB240DD931F84243170A32EB030000[/code]works for me...Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 04:59:35
|
Maybe you are just missing the "0x" in front?Peter LarssonHelsingborg, Sweden |
 |
|
pszivos
Starting Member
10 Posts |
Posted - 2006-10-30 : 04:59:39
|
Yes, this one works nicely for me, too. But, ask I said I go throught each database and the names are referred to from a variable.How do you do a USE @databasename? I could not, this is why I started to construct an EXEC command where I have to operate with strings! |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 05:05:55
|
Try this code of mine/*select * from test..sysusersselect * from master..sysloginsselect * from master..sysdatabases*/create table #db (id int identity(0,1), name sysname)insert #db (name)select namefrom master..sysdatabases--where sid <> 0x01create table #logins (name sysname, sid varbinary(85))insert #logins (name, sid)select name, sidfrom master..sysloginswhere sid <> 0x01create table #users (dbname sysname null, loginsid varbinary(85), username sysname)declare @id int, @sql varchar(1000), @dbname sysnameselect @id = max(id)from #dbwhile @id >= 0 begin select @dbname = name from #db where id = @id select @sql = 'insert #users (loginsid, username) select sid, name from ' + quotename(@dbname) + '..sysusers where sid is not null' exec (@sql) update #users set dbname = @dbname where dbname is null select @id = @id - 1 enddrop table #db-- Logins that are connected to a databaseselect 'Logins that are connected to a database', #users.dbname 'Database name', #logins.name 'Login name'from #usersinner join #logins on #logins.sid = #users.loginsid-- Logins that are not connected to a databaseselect 'Logins that are not connected to a database', #logins.name 'Login name'from #loginsleft join #users on #users.loginsid = #logins.sidwhere #users.loginsid is null-- Users for a database that not have a loginselect 'Users for a database that not have a login', #users.dbname 'Database name', #users.username 'User name'from #usersleft join #logins on #logins.sid = #users.loginsidwhere #logins.sid is nulldrop table #loginsdrop table #users Peter LarssonHelsingborg, Sweden |
 |
|
pszivos
Starting Member
10 Posts |
Posted - 2006-10-30 : 08:45:19
|
Thank you very much! I saved the logins into a tmp table and used it like suggested. It works!Regards,Peter |
 |
|
Niikola
Starting Member
1 Post |
Posted - 2009-02-05 : 03:46:21
|
quote: ...need to find a way to use the binary data as string...
Convert binary values to character stringPrint Convert(varchar(64), 0x4665656442656566, 0)Print Convert(varchar(64), 0x4665656442656566, 1)Print Convert(varchar(64), 0x4665656442656566, 2) Results:FeedBeef0x46656564426565664665656442656566 Convert character string to binaryPrint Convert(varbinary(64), 'FeedBeef', 0)Print Convert(varbinary(64), '0xFeedBeef', 1)Print Convert(varbinary(64), 'FeedBeef', 2) Results:0x46656564426565660xFEEDBEEF0xFEEDBEEF |
 |
|
|