由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - [SQL] just figured out how to loop through all columns in a table
相关主题
SQL run a stored procedure by fetching from a cursor row by (转载)[问题]怎么用proc sql获取row number的值
SQL find distinct values in large table (转载)Sas advance chapter quiz 一问
SQL combine two tables into one table and add a new column (转载)请教 macro variable 和 PROC SQL的问题
compare two large tables SQL (转载)请教一个简单的SQL问题
R:matrix這個SQL sub-query 是否畫蛇添足 ?
dynamic formula construction in R为什么结果是这样?
[Mysql] how to return NULL count in group by querySQL求教:如何求出各组Top N的记录?
help for sas programSQL中啥时候用group by, 啥时候用self-join?
相关话题的讨论汇总
话题: mycursor话题: colname话题: sql话题: select
进入Statistics版参与讨论
1 (共1页)
c***z
发帖数: 6348
1
It took me a whole day. Either I am retarded or SQL is. Why it is so
complicated just to loop over all columns!
I am posting here to help people facing similar problems. If you have a
better solution, please do share! Thanks!
Below is the SQL code:
DECLARE @ColName VARCHAR(150)
DECLARE @sqlStatement VARCHAR(MAX)
SET @sqlStatement = 'initial part of you query, for example select, insert
update etc. '
DECLARE myCursor CURSOR STATIC FOR
SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects
WHERE name = 'Your table name')
OPEN myCursor

FETCH NEXT
FROM myCursor
INTO @ColName

WHILE @@FETCH_STATUS = 0
BEGIN

-- build you query string here
-- from example:
-- set @sqlStatement = 'select' + ' ' + @ColName + ' ' + 'FROM
yourtablename'
SET @sqlStatement = 'select'
SET @sqlStatement = @sqlStatement + ' ' + @ColName + ' ' + 'FROM
yourtablename'

-- execute the query
EXEC(@sqlStatement)

FETCH NEXT
FROM myCursor
INTO @ColName
END

CLOSE myCursor
DEALLOCATE myCursor
m*******g
发帖数: 3044
2
收藏,太好了, 虽然看不懂.
干啥用的?
c***z
发帖数: 6348
3
To pre-process data in SQL before modeling, which is supposed to be faster
than R...

【在 m*******g 的大作中提到】
: 收藏,太好了, 虽然看不懂.
: 干啥用的?

c***z
发帖数: 6348
4
Note: I didn't invent it, a co-worker taught me this
just to clarify
1 (共1页)
进入Statistics版参与讨论
相关主题
SQL中啥时候用group by, 啥时候用self-join?R:matrix
完全不懂SAS,急请教一个问题dynamic formula construction in R
SAS Proc SQL count问题[Mysql] how to return NULL count in group by query
请教一个SQL 的问题help for sas program
SQL run a stored procedure by fetching from a cursor row by (转载)[问题]怎么用proc sql获取row number的值
SQL find distinct values in large table (转载)Sas advance chapter quiz 一问
SQL combine two tables into one table and add a new column (转载)请教 macro variable 和 PROC SQL的问题
compare two large tables SQL (转载)请教一个简单的SQL问题
相关话题的讨论汇总
话题: mycursor话题: colname话题: sql话题: select