1

My below code works fine in SSMS but it is giving me an error while executing using SSRS Incorrect Syntax near ',' Dataset.I am trying to select Multiple Parameters through SSRS.

Can anyone please guide?

Below is my code

Declare @Startdate datetime
Declare @Enddate datetime
Declare @Debug int=0
Declare @TimeZone int=330
Declare @OrganizationId int
Declare @OrganizationName varchar(320)

Set @Startdate ='2016-03-08 00:00:00'
Set @Enddate ='2016-03-09 00:00:00'
--Set @OrganizationName=@POrganization

Declare @Orgname varchar(100) 
declare @OrganizationIds int
set @Orgname='Root Org,Maersk Line'
Declare @place TABLE(ID INT IDENTITY,Places varchar(100))
insert into @place (Places) values(@Orgname)
--select * from @place

Declare @RawData Table 
(
    ROWID int identity(1,1) primary key,
    OrgId int,
    Orgname varchar(100) 
)


;WITH SplitSting AS
(

     SELECT
            ID,LEFT(Places,CHARINDEX(',',Places)-1) AS Part
            ,RIGHT(Places,LEN(Places)-CHARINDEX(',',Places)) AS Remainder
        FROM @place
        WHERE Places IS NOT NULL AND CHARINDEX(',',Places)>0
    UNION ALL
    SELECT
        ID,LEFT(Remainder,CHARINDEX(',',Remainder)-1)
            ,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder))
        FROM SplitSting
        WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0
    UNION ALL
    SELECT
        ID,Remainder,null
        FROM SplitSting
        WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)=0
)

--select  * from SplitSting
insert into @RawData(OrgName,OrgId)
SELECT S.Part,O.ID FROM SplitSting S 
JOIN ORGANIZATION O
ON S.Part=O.NAME

--select * from @RawData

DECLARE @RowCount INT
Declare @String varchar(100)
SELECT @RowCount = COUNT(*) FROM @RawData

WHILE (@RowCount>0)
BEGIN
       set @String=convert(varchar,@OrganizationIds)+','
         If (@RowCount>0)
         Begin  

            PRINT 'Loop Sequence : ' + convert(varchar,@RowCount)  + ' '

            set  @OrganizationIds = (SELECT  OrgId FROM @RawData WHERE ROWID = @RowCount)
                PRINT 'Orgid Inside Loop:' +  Convert(varchar,@OrganizationIds)



End


          Set @RowCount = @RowCount-1


Set  @OrganizationIds =  convert(varchar,@OrganizationIds) 

PRINT 'Orgid Outside Loop:'+   convert(varchar,@OrganizationIds)
set @String=@String + Convert(varchar,@OrganizationIds) 

END
PRINT 'String Value Outside Loop: ' + @String

Declare  @TempData Table
(

    OrganizationID int
)
insert into @TempData(OrganizationID)
EXEC GetFormsData_Organization @String
Rajashri
  • 59
  • 1
  • 5
  • One issue could be that you're calling `convert(varchar,....)` without ever specifying a length. [The default length is 30 in SQL Server](https://msdn.microsoft.com/en-us/library/ms176089.aspx) when not specified, maybe some of the query is being cut off. Not sure why it would work in SSMS and not elsewhere though. – Kidiskidvogingogin Apr 05 '16 at 20:50
  • Also, everytime you get back to the top of the loop you reset `@String` back to `@OrganizationIds + ','` You will only ever print the last two numbers in the sequence. – Kidiskidvogingogin Apr 05 '16 at 21:00
  • Isn't SSRS just expecting a single `select` statement? I think you need a stored procedure. – shawnt00 Apr 06 '16 at 00:01

1 Answers1

0

I am not sure what exactly this code is doing. I have not read it thoroughly.
But as you said this works fine in SSMS then how about putting this code in a Table valued function in SQL Server and then calling that function with parameters from the SSRS DataSet.

Let me know if you need more help to do that.

programmie
  • 41
  • 3
  • Thanks...Can you please explain me in detail how can i used Table valued function to do that? – Rajashri Apr 06 '16 at 06:30
  • Basically, in SSMS under DatabaseName--> Programmability --> Functions --> Table-valued Functions , you create a function say XYZ(parameter1, parameter2, .... ) { <> } then in your dataset you can do Select * from Databasename,.XYZ(parameter1, parameter2 ... ) This link should help - https://technet.microsoft.com/en-us/library/ms191165(v=sql.105).aspx – programmie Apr 06 '16 at 15:34