Quantcast
Viewing latest article 6
Browse Latest Browse All 30

sql command getting truncated after max value is reached

hello everyone, I have a stored procedure where we declared a variable sql with nvarchar(4000) as datatype. Now, this variable stores a sql query coming from a result set of a table.Once the variable data length is reached, query gets truncated and execution of this stored procedure fails. below is the stored procedure: ALTER proc sp_singlevalued_selecttable_attributes_getall @debug bit=0 as declare @mdtype nvarchar(30) declare @tablename nvarchar(30) declare @select nvarchar(30) declare @mdname nvarchar(30) declare @sql nvarchar(4000) declare @counter INT declare @rows INT declare @resultTable table ( [indx] int identity (1, 1) not null primary key, [mdname] [nvarchar] (30), [tablename] [nvarchar](30) , [mdtype] [nvarchar] (30) , [select] [nvarchar] (30) ) SET NOCOUNT ON -- Get the RDS_DATA table from RdsData..RDS_MAP insert into @resultTable (mdname, mdtype, tablename, [select]) select MD_NAME, MD_TYPE, 'RdsData..' + RDS_DATA_TABLE as RDS_DATA_TABLE, 'RdsData..' + RDS_SELECT_TABLE as RDS_SELECT_TABLE from RdsData..RDS_MAP where (MD_MULTI_VALUE=0 and RDS_SELECT_TABLE !='') and BASE_TABLE = 'Asset' order by MD_NAME, MD_APPID -- Build the dynamic query select @sql = '' select @counter = 1 select @rows = COUNT(*) from @resultTable while (@counter <= @rows) begin select @mdname = mdname, @mdtype = mdtype, @tablename = tablename, @select = [select] from @resultTable where indx =@counter -- see if joining select table for code resolution is necessary set @sql = @sql + 'select UID, MD_KEY, TAG as VALUE, t1.UPDATE_STAMP,'''+@mdname+''' as MD_NAME, ' +@mdtype+' as MD_TYPE from ' + @tablename + ' t1 join ' + @select + ' t2 on t1.' + @mdname + ' = t2.TVALUE' if (@counter < @rows) select @sql = @sql + ' union all ' select @counter =@counter + 1 end exec sp_executesql @sql if @debug = 1 print @sql SET NOCOUNT OFF please suggest. regards, Mayank

Viewing latest article 6
Browse Latest Browse All 30

Trending Articles