Author |
Topic |
willfindavid
Starting Member
27 Posts |
Posted - 2009-01-28 : 01:18:46
|
I use the following code,SELECT @strXml = '<AccessRights projectId="'+CAST(@ProjectID AS VARCHAR(100))+'" userId="'+CAST(@UserID AS VARCHAR(100))+'">' SELECT @strXml = @strXml+'<object Id="' + CAST(scr.objectCode as varchar(100)) + '" value="' + CASE WHEN AccessRight = 1 THEN 'N' WHEN AccessRight = 2 then 'V' WHEN AccessRight = 3 then 'F' ELSE 'V' END+'"></object>' FROM #screens scr left join #access acc ON acc.objectCode = scr.objectCodeSELECT @strXml = @strXml+'</AccessRights>' SELECT @strXml AS ResultSet When i use the select for displaying the xml string it shows,An error occurred while executing batch. Error message is: Invalid calling sequence: file stream must be initialized first. |
|
AvanthaSiriwardana
Yak Posting Veteran
78 Posts |
Posted - 2009-01-28 : 01:32:03
|
Declare @strXml First and then assign the SELECT statement to @strXmlAvantha Siriwardana |
|
|
willfindavid
Starting Member
27 Posts |
Posted - 2009-01-28 : 01:39:10
|
i have declared @strXml as,DECLARE @strXml varchar(max) |
|
|
AvanthaSiriwardana
Yak Posting Veteran
78 Posts |
Posted - 2009-01-28 : 01:45:19
|
Declare it as DECLARE @strXml TEXTAvantha Siriwardana |
|
|
willfindavid
Starting Member
27 Posts |
Posted - 2009-01-28 : 02:07:31
|
An Error occurs if @strXml is declared as text,The text, ntext, and image data types are invalid for local variables. |
|
|
AvanthaSiriwardana
Yak Posting Veteran
78 Posts |
Posted - 2009-01-28 : 02:45:48
|
but where is your xml path??Avantha Siriwardana |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
willfindavid
Starting Member
27 Posts |
Posted - 2009-01-28 : 04:15:05
|
Can anyone tell how to generate xml like,<AccessRights projectId="4296" userId="12345"> <object Id="270" value="V" /> <object Id="156" value="V" /> <object Id="157" value="V" /> <object Id="158" value="V" /> <object Id="159" value="V" /> <object Id="160" value="V" /> <object Id="161" value="V" /> <object Id="162" value="V" /> <object Id="163" value="V" /> <object Id="164" value="V" /> <object Id="165" value="V" /> <object Id="54" value="V" /></AccessRights>with projectId and userId provided as input parametersand object id and value retrived from the tables #Screens and #Access using left join where #Access.objectCode = #Screens.objectCode |
|
|
willfindavid
Starting Member
27 Posts |
Posted - 2009-01-28 : 04:18:37
|
I have tried like this but cannot able to display the attributes projectId and userId for the root nodeSELECT object.objectCode 'Id',CASE WHEN AccessRight = 1 THEN 'N'WHEN AccessRight = 2 then 'V'WHEN AccessRight = 3 then 'F' ELSE 'V' END 'value'FROM #screens object left join #access acc ON acc.objectCode = object.objectCode FOR XML AUTO,ROOT('AccessRights') |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-28 : 06:52:54
|
[code]DECLARE @ProjectID INT, @UserID INTSELECT @ProjectID = 4296, @UserID = 12345DECLARE @Sample TABLE ( ObjectCode INT, AccessRight INT )INSERT @Sample ( ObjectCode, AccessRight )SELECT 270, 2 UNION ALLSELECT 270, 4 UNION ALLSELECT 156, 5 UNION ALLSELECT 157, 2 UNION ALLSELECT 158, 4 UNION ALLSELECT 159, 5 UNION ALLSELECT 160, 2 UNION ALLSELECT 161, 4 UNION ALLSELECT 162, 5 UNION ALLSELECT 163, 2 UNION ALLSELECT 164, 4 UNION ALLSELECT 165, 5 UNION ALLSELECT 54, 2DECLARE @x XMLSET @x = ( SELECT ObjectCode AS [@Id], CASE AccessRight WHEN 1 THEN 'N' WHEN 3 then 'F' ELSE 'V' END AS [@value] FROM @Sample FOR XML PATH('object') )set @x = '<AccessRights projectID="' + CAST(@ProjectID AS VARCHAR(11)) + '" userID="' + CAST(@UserID AS VARCHAR(11)) + '">' + CAST(@x AS VARCHAR(MAX)) + '</AccessRights>'SELECT @x[/code] E 12°55'05.63"N 56°04'39.26" |
|
|
|