WRITE A FUNCTION TO DISPLAY THE OUTPUT OF AN EXISTING TABLE
RANGE LIKE COMMAM SEPERATED VALUES LIKE RANGE1,RANGE2,...

Answers were Sorted based on User's Feedback



WRITE A FUNCTION TO DISPLAY THE OUTPUT OF AN EXISTING TABLE RANGE LIKE COMMAM SEPERATED VALUES LIK..

Answer / carmel franco

Create function int select_dynamic(range varchar(255))

As
declare
@Qry Varchar(2000)
Begin
Begin try
Set @Qry =”select * from table1 where col1
in (“+range+”)”
Exec @qry
Catch
Print “error processing parameter”
Return -1
End try

Return 1
END

Is This Answer Correct ?    2 Yes 0 No

WRITE A FUNCTION TO DISPLAY THE OUTPUT OF AN EXISTING TABLE RANGE LIKE COMMAM SEPERATED VALUES LIK..

Answer / jk.garg25

you can write this query in table valued function

DECLARE @RangeValue As VARCHAR(MAX)
SET @RangeValue = '1,2,'

DECLARE @Qry As VARCHAR(MAX)

DECLARE @ResData AS VARCHAR(MAX)
DECLARE @InData AS VARCHAR(MAX)

DECLARE @rId AS INT
DECLARE @Pos AS INT

SET @ResData = ''
SET @InData = ''
WHILE(LEN(@RangeValue)>1)
BEGIN
SET @Pos = CHARINDEX(',',@RangeValue,1)
SET @rId = SUBSTRING(@RangeValue,1,@Pos-1)
SET @ResData = @ResData + CASE WHEN @ResData <> '' THEN ','
ELSE '' END + 'ISNULL(['+ CAST(@rId AS VARCHAR(5)) + '],0)
AS Range' + CAST(@rId AS VARCHAR(5))
SET @InData = @InData + CASE WHEN @InData <> '' THEN ','
ELSE '' END + '['+ CAST(@rId AS VARCHAR(5)) + ']'
SET @RangeValue=SUBSTRING(@RangeValue,@Pos+1,LEN(@RangeValue))
END

SET @Qry =
'SELECT '
+ @ResData +
' FROM
(
SELECT Col1 FROM table1(NoLock)
) p
PIVOT
(
MAX (Col1) FOR Col1 IN (' + @InData + ')
) AS pvt'

--PRINT (@Qry)
EXEC (@Qry)

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More SQL Server Interview Questions

What is inline table-value user-defined function?

0 Answers  


What does normalization do to data?

0 Answers  


What is exporting utility?

0 Answers  


What are key, name and value columns of an attribute? : sql server analysis services, ssas

0 Answers  


What number aggregate functions are accessible there in sql?

0 Answers  






What do you understand by a view? What does the with check option clause for a view do?

0 Answers  


How to test a dml trigger in ms sql server?

0 Answers  


What are the differences between left join and inner join in sql server?

0 Answers  


How many categories of functions based their return modes?

0 Answers  


Explain what is the function of sql server agent windows service?

0 Answers  


What are explicit and implicit transactions?

4 Answers   BirlaSoft,


What is the optimization being performed in oracle and SQL Server?

0 Answers   Cap Gemini,


Categories