楼主: reduce_fat
994 22

SQL Loop 重金悬赏求解答 [推广有奖]

11
reduce_fat 发表于 2015-11-18 10:08:32
yongyitian 发表于 2015-11-18 10:05
Try to creat a STORED PROCEDURE with parameters using the

query in the picture.
Did you mean to use "with statement" to create temp tables, and then call later?

Or did you mean this https://msdn.microsoft.com/en-us/library/ms345415.aspx ?

I want to see a loop that only define those variables at the beginning of the procedure, and then I can make as many tables I need for the same procedure on 20 or more problems.

Thanks.

12
yongyitian 发表于 2015-11-18 10:34:37
You may try the example in the part Using Transact-SQL in the page.  The parameter start with @

Replace code between AS and GO with your query.

There is a WHILE loop in the SQL but you need a control variable-value to stop the loop.  Search SQL while loop to see the examples.

13
prince315 在职认证  发表于 2015-11-18 11:00:19
没看具体问题,不过如果是只是没在SQL里面写过Loop,简单share几个结果,看是否有帮助(不是SP)。常用的有while或者用游标实现:
  1. declare @i int, @t int
  2. declare @colname varchar(100), @oldcol varchar(100), @newcol varchar(100), @pre varchar(50)

  3. set @pre = 'GA_'

  4. if OBJECT_ID('PR_GrantAward') is not null drop table PR_GrantAward
  5. select * into PR_GrantAward from GrantAward

  6. set @i = 1
  7. set @t = (select max(column_id) from sys.columns where object_name(object_id) = 'GrantAward')

  8. while @i <= @t
  9. begin
  10. set @colname = (select name from sys.columns where object_name(object_id) = 'GrantAward' and column_id = @i)
  11. print @colname
  12. set @oldcol = 'PR_GrantAward.['+@colname+']'
  13. set @newcol = @pre+@colname
  14. print @oldcol
  15. print @newcol
  16. exec sp_rename @oldcol,@newcol,'COLUMN'
  17. set @i = @i+1
  18. end

  19. select top 5 * from PR_GrantAward

  20. -- Cursor
  21. declare @id int;
  22. declare @name nvarchar(50);
  23. declare v_cursor cursor for (
  24.         select V_id, v_name from #v_test
  25.         where right(v_name,2)='10' )
  26. open v_cursor
  27. fetch next from v_cursor into @id, @name

  28. while (@@fetch_status=0)
  29. begin
  30.         print cast(@id as nchar(3)) + @name
  31.         fetch next from v_cursor into @id, @name
  32. end
  33. close v_cursor
  34. deallocate v_cursor
复制代码
已有 1 人评分经验 收起 理由
reduce_fat + 71 观点有启发

总评分: 经验 + 71   查看全部评分

14
reduce_fat 发表于 2015-11-18 11:06:27
prince315 发表于 2015-11-18 11:00
没看具体问题,不过如果是只是没在SQL里面写过Loop,简单share几个结果,看是否有帮助(不是SP)。常用的有 ...
明天试验一下。 另外需要和战友讨论所用方法有哪些不同。 如果我能把这个过程简化了,就不用在用ctrl+f 和 r 去替代每个复制的变量了。

15
sniperhgy 发表于 2015-11-18 11:35:28
reduce_fat 发表于 2015-11-18 10:04
我没有在SQL里写过loop 能说的详细一些吗? 用with statement? 不是Oracle 而是Sql server。 最 ...
可以写存储过程,也可以用loop语句,这是参考:http://www.techonthenet.com/sql_server/loops/for_loop.php

16
reduce_fat 发表于 2015-11-20 13:23:11
sniperhgy 发表于 2015-11-18 11:35
可以写存储过程,也可以用loop语句,这是参考:http://www.techonthenet.com/sql_server/loops/for_loop. ...
写完运行时候才发现自己没有权限使用存储过程 create procedure 只能再想loop啦。

17
teqel 发表于 2015-11-20 22:53:14
大侠们的sql很厉害啊,膜拜一下

18
reduce_fat 发表于 2015-11-21 08:05:42
prince315 发表于 2015-11-18 11:00
没看具体问题,不过如果是只是没在SQL里面写过Loop,简单share几个结果,看是否有帮助(不是SP)。常用的有 ...
这个loop 在运转的时候怎么确保不同问题的变化?比如说我有50个问题,problem_desc将是需要运行的主变量,其他步骤全都围绕着它。 我看stored procedure 更适合解决这个问题,但是我没有权限自己创建procedure。 只需要把所有步骤存储一下,然后每次call problem_desc procedure 的时候改变问题就好了。 这个也能loop吗?

19
sniperhgy 发表于 2015-11-21 13:05:48
reduce_fat 发表于 2015-11-21 08:05
这个loop 在运转的时候怎么确保不同问题的变化?比如说我有50个问题,problem_desc将是需要 ...
procedure也可以循环的。

20
reduce_fat 发表于 2015-11-24 11:35:15
prince315 发表于 2015-11-18 11:00
没看具体问题,不过如果是只是没在SQL里面写过Loop,简单share几个结果,看是否有帮助(不是SP)。常用的有 ...
这个好是好,但是我的problem_desc 是唯一需要变化的量,每次运转整个过程的时候, 都要把problem_desc替换成新的。 就好比先把procedure 存储起来,然后每次call的时候再换problem_desc就行了。这个loop 要怎么在SQL里实现。 谢谢。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 08:49