Results 1 to 6 of 6
  1. #1
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071

    SQL Server Functioln problem


    I have a sql server function that queries a table to get a value for a report. The function looks like this.

    Code:
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    -- =============================================
    -- Author:		<Author,,Name>
    -- Create date: <Create Date, ,>
    -- Description:	<Description, ,>
    -- =============================================
    ALTER FUNCTION [dbo].[benchmark]
    (
    	-- Add the parameters for the function here
    	@strlob varchar(25),
    	@typeid int)
    
    RETURNS  varchar(255)
    AS
    BEGIN
    	-- Declare the return variable here
    	DECLARE @bchmark varchar(25)
    	Declare @tmpstr varchar(25)
    	--declare @selectstr varchar(254)
    	set @tmpstr = Case @typeid
    		when 1 Then '2,16,1'
    		when 2 then '10,15,12'
    		When 3 then  '3,4'
    		when 4 then  '9,14'
    		when 5 then  '11,13'
    		end
    
    	-- Add the T-SQL statements to compute the return value here
    	--set @selectstr = 'SELECT Benchmark from hvvmgrpt.dbo.tbl_benchmarks2 where LOB = @strlob and ID In (' + @tmpstr  + ')'
    
    	set @bchmark = (SELECT Benchmark from hvvmgrpt.dbo.tbl_benchmarks2 where LOB = @strlob and ID In ( @tmpstr ))
    
    	-- Return the result of the function
    	RETURN  @bchmark
    
    
    END
    
    The problem is in the end of the set statement.  and ID in ( @tmpstr))  The sql looks fine but instead of looking at the numbers  for example when @typeid = 3 then then I'm looking for the ID number of either 3 or 4.  Which works perfectly when I run the query in ssms but is looking at the 3,4 as a string instead of 2 numbers. since tempvar is a string how do I fix this??

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,944
    Please edit your post so the narrative below code is not between CODE tags. And when you say "tempvar", did you mean "tmpstr"?

    bchmark is declared as a varchar(25) - how can you set it to a recordset or a string of more than 25 characters, whichever that set is attempting?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Sorry thought I had put that after the code tag. yes tmpstr it's not the value being returned is < 25 characters. It kept erroring and I was trying to figure out what the problem was so I made some changes to return the query string instead of the value and forgot to make sure I change everything back. Getting a little frustrated and now making stupid mistakes. I'm going to work on something else and will look at this again tomorrow. Sorry

  4. #4
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,045
    Try something like:

    Code:
    select @bnchmark =  Benchmark from hvvmgrpt.dbo.tbl_benchmarks2  cross apply string_split(@tmpstr,',') where ID = value and LOB = @strlob
    remark: if the query returns more than 1 result, your function will return the first found value

  5. #5
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Quote Originally Posted by NoellaG View Post
    Try something like:

    Code:
    select @bnchmark =  Benchmark from hvvmgrpt.dbo.tbl_benchmarks2  cross apply string_split(@tmpstr,',') where ID = value and LOB = @strlob
    remark: if the query returns more than 1 result, your function will return the first found value
    Nope get this error:
    Msg 208, Level 16, State 1, Line 1
    Invalid object name 'string_split'.

    Completion time: 2024-04-25T09:25:12.5968019-07:00

  6. #6
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    answered a little to quickly. Looked up string_split and it requires a compatibility levvel of 130. So I checked mine and it was set to 100 ran the following query
    Code:
    ALTER DATABASE AdventureWorks2022  
    SET COMPATIBILITY_LEVEL = 130;  
    GO
    now the function works thank you

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Problem with UPDATE query with SQL Server backend
    By James A. Fortune in forum SQL Server
    Replies: 6
    Last Post: 03-05-2021, 03:51 AM
  2. Server corruption problem
    By Jayshe9999 in forum Access
    Replies: 2
    Last Post: 02-23-2019, 08:32 AM
  3. Replies: 7
    Last Post: 02-02-2018, 06:17 AM
  4. Hyperlink Problem in SQL SERVER!!
    By Rxp in forum Access
    Replies: 0
    Last Post: 05-03-2012, 07:48 PM
  5. Split Database on a Server Problem
    By DianeG in forum Access
    Replies: 1
    Last Post: 05-29-2010, 01:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums