GVKun编程网logo

SQL in(@Variable)查询(sql语句in查询)

30

最近很多小伙伴都在问SQLin和@Variable查询这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展$VARIABLE和${VARIABLE}之间有什么区别、(@Variable

最近很多小伙伴都在问SQL in@Variable查询这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展$ VARIABLE和$ {VARIABLE}之间有什么区别、(@Variable)查询中的SQL、C中的* *之间是否有区别?、Graphql、GQL 和 InputTypes {"message":"Variable "$variable" 所需类型 "type!" 未提供"}等相关知识,下面开始了哦!

本文目录一览:

SQL in(@Variable)查询(sql语句in查询)

SQL in(@Variable)查询(sql语句in查询)

我有以下代码,问题是我的变量列表@LocationList本质上是一个csv字符串。当我将其用作(@LocationList)中whereLocationID的一部分时,它说它不是int(LocationID是int)。我怎样才能使该csv字符串在子句中被接受?

Declare @LocationList varchar(1000)Set @LocationList = ''1,32''select Locations from table where Where LocationID in (@LocationList)

答案1

小编典典

最有效的方法是使用动态SQL,例如提到rt2800(带有Michael Allen的注入警告)

但是,您可以创建一个函数:

ALTER  FUNCTION [dbo].[CSVStringsToTable_fn] ( @array VARCHAR(8000) )RETURNS @Table TABLE ( value VARCHAR(100) )AS     BEGIN        DECLARE @separator_position INTEGER,            @array_value VARCHAR(8000)        SET @array = @array + '',''        WHILE PATINDEX(''%,%'', @array) <> 0             BEGIN                SELECT  @separator_position = PATINDEX(''%,%'', @array)                SELECT  @array_value = LEFT(@array, @separator_position - 1)                INSERT  @Table                VALUES  ( @array_value )                SELECT  @array = STUFF(@array, 1, @separator_position, '''')            END        RETURN    END

并从中选择:

DECLARE @LocationList VARCHAR(1000)SET @LocationList = ''1,32''SELECT  Locations FROM    tableWHERE   LocationID IN ( SELECT   *                           FROM     dbo.CSVStringsToTable_fn(@LocationList) )

或者

SELECT  LocationsFROM    table loc        INNER JOIN dbo.CSVStringsToTable_fn(@LocationList) list            ON list.value = loc.LocationID

当您尝试从SSRS向PROC发送多值列表时,这非常有用。

$ VARIABLE和$ {VARIABLE}之间有什么区别

$ VARIABLE和$ {VARIABLE}之间有什么区别

任何人都可以请给我一个解释,为什么一些Linux专家build议我们在Bash脚本中使用$ {VARIABLE}? 似乎没有任何区别。

不能使用任何环境variables

为什么不从bash脚本导出工作

程序使用过时的(不是当前的)envvariables值

shell和环境variables之间的区别

Debian $ PATHvariables的变化

假设你想打印$VARIABLE ,紧接着是"string"

echo "$VARIABLEstring" # tries to print the variable called VARIABLEstring echo "${VARIABLE}string" # prints $VARIABLE and then "string"

Bash还支持使用此语法的字符串操作 。

你可能想要这样做的一个原因是作为分隔符:

a=42 echo "${a}sdf" # 42sdf echo "$asdf" # prints nothing because there's no variable $asdf

这个功能通常用来保护周围字符的变量名。

$ var=foo

如果我们希望在$var的末尾连接一个字符串我们不能这样做:

$ echo $varbar $

因为这是试图使用一个新的变量$varbar 。

相反,我们需要将{} var {}括在{}中:

$ echo ${var}bar foobar

(@Variable)查询中的SQL

(@Variable)查询中的SQL

我有以下代码,问题是我的变量列表@LocationList本质上是一个csv字符串.当我使用它作为(@LocationList)中LocationID的一部分时,它表示它不是一个int(LocationID是一个i​​nt).如何让这个csv字符串被teh in子句接受?
Declare @LocationList varchar(1000)
Set @LocationList = '1,32'

select Locations from table where Where LocationID in (@LocationList)

解决方法

最有效的方法是使用动态sql,例如rt2800提及(由迈克尔艾伦注射警告)

但是你可以做一个功能:

ALTER  FUNCTION [dbo].[CSVStringsToTable_fn] ( @array VARCHAR(8000) )
RETURNS @Table TABLE ( value VARCHAR(100) )
AS 
    BEGIN
        DECLARE @separator_position INTEGER,@array_value VARCHAR(8000)  

        SET @array = @array + ','

        WHILE PATINDEX('%,%',@array) <> 0 
            BEGIN
                SELECT  @separator_position = PATINDEX('%,@array)
                SELECT  @array_value = LEFT(@array,@separator_position - 1)

                INSERT  @Table
                VALUES  ( @array_value )

                SELECT  @array = STUFF(@array,1,@separator_position,'')
            END
        RETURN
    END

并从中选择:

DECLARE @LocationList VARCHAR(1000)
SET @LocationList = '1,32'

SELECT  Locations 
FROM    table
WHERE   LocationID IN ( SELECT   *
                           FROM     dbo.CSVStringsToTable_fn(@LocationList) )

要么

SELECT  Locations
FROM    table loc
        INNER JOIN dbo.CSVStringsToTable_fn(@LocationList) list
            ON list.value = loc.LocationID

当您尝试从SSRS向PROC发送多值列表时,这非常有用.

C中的* <variable>和<variable> *之间是否有区别?

C中的* *之间是否有区别?

在此声明中

my_struct * create(char *name);

函数的名称为create。该函数具有一个类型为char *的参数,而该函数具有返回类型my_struct *。即它返回一个指向my_struct类型的对象的指针。

my_struct *和* name中*的含义是否存在差异

my_struct是类型说明符。因此my_struct *是指针类型。 name是表示参数名称的标识符。参数(标识符)name的类型为char *

请注意,这些参数声明是相同的

char* name
char * name
char *name

该参数的类型为char *,该参数的名称为name

在不同时的函数声明中,可以省略其参数定义名称。

所以上面的函数声明也可以写成

my_struct * create( char * );

Graphql、GQL 和 InputTypes {

Graphql、GQL 和 InputTypes {"message":"Variable "$variable" 所需类型 "type!" 未提供"}

如何解决Graphql、GQL 和 InputTypes {"message":"Variable \"$variable\" 所需类型 \"type!\" 未提供"}?

我无法使用 gql 将正确的输入类型发送到我的 graphQL 服务器。

我的 typeDefs 服务器端:

export const typeDefs = gql`
  input AuthorizeInput {
    username: String!
    password: String!
  }

  type AuthorizationPayload {
    user: User!
    accesstoken: String!
    expiresAt: DateTime!
  }

  extend type Mutation {
    """
    Generates a new access token,if provided credentials (username and password) match any registered user.
    """
    authorize(credentials: AuthorizeInput): AuthorizationPayload
  }
`;

我的 Mutation 服务器端:

export const resolvers = {
  Mutation: {
    authorize: async (obj,args,{ authService }) => {
      console.log(''args'',args);
      const {
        credentials: { username,password },} = await argsSchema.validate(args,{
        stripUnkNown: true,});

      const user = await User.query().findOne({ username });

      if (!user) {
        throw new UserInputError(''Invalid username or password'');
      }

      const match = await bcrypt.compare(password,user.password);

      if (!match) {
        throw new UserInputError(''Invalid username or password'');
      }

      return {
        user,...authService.createAccesstoken(user.id),};
    },},};

我的方案验证与 yup 服务器端:

const argsSchema = yup.object().shape({
  credentials: yup.object().shape({
    username: yup.string().required().lowercase().trim(),password: yup.string().required().trim(),}),});

我从前端的 gql 突变:

export const AUTHORIZE = gql`
  mutation authorize($credentials: AuthorizeInput!) {
    authorize(credentials: $credentials ) {
      accesstoken
    }
  }
`;

我从前端执行突变的钩子:

const useSignIn = () => {
  const [mutate,result] = useMutation(AUTHORIZE);

  const signIn = async ({ username,password }) => {
    mutate({ credentials: { username,password } });
  };

  return [signIn,result];
};

当我在后台登录收到的参数时,我得到:

args {
  credentials: [Object: null prototype] { username: ''kalle'',password: ''password'' }
}

但架构验证不正常并返回:

{"message":"Variable \"$username\" of required type \"String!\" was not 
provided."

{"message":"Variable \"$password\" of required type \"String!\" was not 
provided."

但在 graphql 游乐场中,当我启动此突变时,它起作用了:

mutation {
  authorize(credentials: { username: "kalle",password: "password" }) {
    accesstoken
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

关于SQL in@Variable查询的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于$ VARIABLE和$ {VARIABLE}之间有什么区别、(@Variable)查询中的SQL、C中的* *之间是否有区别?、Graphql、GQL 和 InputTypes {"message":"Variable "$variable" 所需类型 "type!" 未提供"}的相关信息,请在本站寻找。

本文标签: