[SQL] Want to declare a string array variable? Declare a table variable instead.


Sometimes we want to declare a array to represent the condition of WHERE IN.
But you can not declare a array in SQL, instead, you can try to declare a table variable with the value you want to put in.

DECLARE @usernameArray TABLE (username varchar(50))
INSERT INTO @usernameArray VALUES ('M12345'), ('M12346'), ('M12347'), ('M12348'), ('M12349')

SELECT * FROM member_info m WHERE  m.username IN 
(SELECT username FROM @usernameArray)

In the code block, I declared a table variable, and inserted 5 rows into the variable.
Then when I needed to select with the condition.
I selected the array from the table variable we declared before (@usernameArray).

#SQL






你可能感興趣的文章

路由器router 路由設計routing 路由route

路由器router 路由設計routing 路由route

[FE102] 前端必備:如何在瀏覽器上儲存資料?

[FE102] 前端必備:如何在瀏覽器上儲存資料?

[筆記] 在使用 Vite 創建的 Vue 3 專案中加入 Eslint(使用 Airbnb 規範)

[筆記] 在使用 Vite 創建的 Vue 3 專案中加入 Eslint(使用 Airbnb 規範)






留言討論