日期:2014-05-18 浏览次数:20475 次
create function dbo.GetTableName ( @tableName nvarchar(250) )returns nvarchar(150) as --.........
select PATINDEX ( '%[%]', '[schema1].[mytab2]' )
DECLARE @str VARCHAR(20) SET @str = '[dbo].[tab1]' IF (CHARINDEX('.',@str) > 0) SET @str = STUFF(@str,1,CHARINDEX('.',@str),'') IF (CHARINDEX('[',@str) > 0) SET @str = STUFF(@str,1,CHARINDEX('[',@str),'') IF (CHARINDEX(']',@str) > 0) SET @str = LEFT(@str,CHARINDEX(']',@str)-1) SELECT @str
------解决方案--------------------
if not object_id('tb') is null drop table tb Go Create table tb([col] nvarchar(18)) Insert tb select N'[schema1].[mytab2]' union all select N'[myschema].[tab1]' union all select N'dbo.tab1' Go Select replace(replace(right([col],len([col])-charindex('.',[col])),']',''),'[','') from tb /* ---------------------------------------------------------------------------------------------------------------- mytab2 tab1 tab1 (3 個資料列受到影響) */
------解决方案--------------------
-- sql 2005 select PARSENAME('dbo.tab1',1); -- sql 2000 declare @tabname sysname; set @tabname='dbo.tab1'; select right(@tabname,charindex('.',reverse(@tabname))-1);