日期:2014-05-18 浏览次数:20622 次
--小梁滴 CREATE PROCEDURE dbo.FindString @string NVARCHAR(100) AS DECLARE @SQL NVARCHAR(4000); SET @SQL = N' DECLARE @str NVARCHAR(4000); SELECT @str = ISNULL(@str + N'' OR '' + c.name + N'' LIKE N''''%' + @string + ' %'''''', c.name + N'' LIKE N''''%' + @string +'%'''''') FROM syscolumns AS c JOIN systypes AS t ON c.id=OBJECT_ID(''?'') AND c.xtype=t.xtype AND t.name IN(''varchar'',''char'',''nvarchar'',''nchar''); SET @str = ''SELECT TOP 1 1 FROM ? WHERE ''+@str; CREATE TABLE #tb(a int); INSERT #tb(a) EXEC(@str); IF EXISTS(SELECT * FROM #tb) PRINT ''?'' '; EXEC sp_MsforeachTable @SQL; GO --查询 declare @str varchar(20) select @str = char(13)+char(10) EXEC dbo.FindString @str
------解决方案--------------------
查字符可以 不知道查char(13)怎么查
------------------第一种方法----------------------
CREATE PROC sp_ValueSearch
@value sql_variant, --要搜索的数据
@precision bit=1 --1=仅根据sql_variant中的数据类型查找对应类型的数据列. <>1,查询兼容的所有列,字符数据使用like匹配
AS
SET NOCOUNT ON
IF @value IS NULL RETURN
--数据类型处理
SELECT xtype INTO #t FROM systypes
WHERE name=SQL_VARIANT_PROPERTY(@value,N'BaseType')
--扩展数据类型及查询处理语句
DECLARE @sql nvarchar(4000),@sql1 nvarchar(4000)
IF @precision=1
SET @sql=CASE SQL_VARIANT_PROPERTY(@value,N'BaseType')
WHEN N'text' THEN N' LIKE N''%''+CAST(@value as varchar(8000))+''%'''
WHEN N'ntext' THEN N' LIKE ''%''+CAST(@value as nvarchar(4000))+''%'''
ELSE N'=@value' END
ELSE
BEGIN
SET @sql=CAST(SQL_VARIANT_PROPERTY(@value,N'BaseType') as sysname)
IF @sql LIKE N'%char' or @sql LIKE N'%text'
BEGIN
INSERT #t SELECT xtype FROM systypes
WHERE name LIKE N'%char' or name LIKE N'%text'
SELECT @sql=N' LIKE N''%''+CAST(@value as '
+CASE
WHEN LEFT(@sql,1)=N'n' THEN ' nvarchar(4000)'
ELSE 'varchar(8000)' END
+N')+N''%'''
END
ELSE IF @sql LIKE N'%datetime'
BEGIN
INSERT #t SELECT xtype FROM systypes
WHERE name LIKE N'%datetime'
SET @sql=N'=@value'
END
ELSE IF @sql LIKE N'%int'
OR @sql LIKE N'%money'
OR @sql IN(N'real',N'float',N'decimal',N'numeric')
BEGIN
INSERT #t SELECT xtype FROM systypes
WHERE name LIKE N'%int'
OR name LIKE N'%money'
OR name IN(N'real',N'float',N'decimal')
SET @sql=N'=@value'
END
ELSE
SET @sql=N'=@value'
END
--保存结果的临时表
CREATE TABLE #(TableName sysname,FieldName sysname,Type sysname,SQ