Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

how to create a function with dynamic sql or any better way to achieve this?

$
0
0

        Hello,

        I have created below SQL query which works fine however when scalar function created ,it
        throws an error "Only functions and extended stored procedures can be executed from within a
        function.". In below code First cursor reads all client database names and second cursor
        reads client locations.

                  DECLARE @clientLocation nvarchar(100),@locationClientPath nvarchar(Max);
                  DECLARE @ItemID int;
                  SET @locationClientPath = char(0);
                  SET @ItemID = 67480;

   --building dynamic sql to replace database name at runtime
         DECLARE @strSQL nvarchar(Max);
         DECLARE @DatabaseName nvarchar(100);
         DECLARE @localClientPath nvarchar(MAX) ;
  
                  Declare databaselist_cursor Cursor for select [DBName] from [DataBase].[dbo].
                  [tblOrganization] 
        
                  OPEN databaselist_cursor
                  FETCH NEXT FROM databaselist_cursor INTO @DatabaseName
   
                  WHILE @@FETCH_STATUS = 0
                  BEGIN       
   PRINT 'Processing DATABASE: ' + @DatabaseName;

    SET @strSQL = 'DECLARE organizationlist_cursor CURSOR
    FOR SELECT '+ @DatabaseName +'.[dbo].[usGetLocationPathByRID]
                               ([LocationRID]) 
    FROM '+ @DatabaseName +'.[dbo].[tblItemLocationDetailOrg] where
                               ItemId = '+ cast(@ItemID as nvarchar(20))  ;
    
     EXEC sp_executesql @strSQL;
   
   
           
    -- Open the cursor
    OPEN organizationlist_cursor
    SET @localClientPath = '';
       
    -- go through each Location path and return the 
     FETCH NEXT FROM organizationlist_cursor into @clientLocation
     WHILE @@FETCH_STATUS = 0
      BEGIN
       SELECT @localClientPath =  @clientLocation; 
       SELECT @locationClientPath =
                                                     @locationClientPath + @clientLocation + ','
       FETCH NEXT FROM organizationlist_cursor INTO
                                                     @clientLocation
      END
       PRINT 'current databse client location'+  @localClientPath;
     -- Close the Cursor
     CLOSE organizationlist_cursor;
     DEALLOCATE organizationlist_cursor;
     FETCH NEXT FROM databaselist_cursor INTO @DatabaseName
    
                END
        
                CLOSE databaselist_cursor;
                DEALLOCATE databaselist_cursor;
     
                -- Trim the last comma from the string
               SELECT @locationClientPath = SUBSTRING(@locationClientPath,1,LEN(@locationClientPath)-  1);
     
                 PRINT @locationClientPath;

        I would like to create above query in function so that return value would be used in 
        another query select statement and I am using SQL 2005.
        I would like to know if there is a way to make this work as a function or any better way
        to  achieve this?

        Thanks,


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>