sql server - sql query that searches all columns in two tables, i need to add a another column "AS" to it -
below sql query searches columns in 2 tables (i didnt make this) need add statement
, (tblusers.forename + ' ' + tblusers.surname) cleanname
but unsure put it, can me out? thanks
use [itapp] go /****** object: storedprocedure [dbo].[sp_searchalltables] script date: 07/11/2013 10:57:43 ******/ set ansi_nulls on go set quoted_identifier on go alter proc [dbo].[sp_searchalltables] ( @searchstr nvarchar(255) ) begin declare @where varchar(8000) declare @sql varchar(8000) set @sql = 'select * tblusers u join tblequipment e on e.userid = u.id 1 = 1 , ( 1= 0 ' select @where = coalesce(@where ,'' ) + ' or ' + case when object_name(object_id) = 'tblusers' 'u' else 'e' end + '.[' + name + '] ''%' + replace(@searchstr, '''', '''''') + '%'' ' sys.columns object_id in ( select object_id sys.objects name in ( 'tblusers','tblequipment' )) , collation_name not null set @where = coalesce(@where, '') + ')' print @sql print @where exec(@sql + @where) end
change:
set @sql = 'select * tblusers u join tblequipment e on e.userid = u.id 1 = 1 , ( 1= 0 '
to
set @sql = 'select u.*,e.*, (u.forename + '' '' + u.surname) cleanname tblusers u join tblequipment e on e.userid = u.id 1 = 1 , ( 1= 0 '
note: need delimit single-quotes..
Comments
Post a Comment