Showing posts with label language. Show all posts
Showing posts with label language. Show all posts

Thursday, February 16, 2012

"for" loop

hello

Does exist in SQL language "for" loop ? If yes, what syntax does it has ?

best wishes
AdamNo - see WHILE and "Cursors" in Books Online. But in SQL, a loop is
often not a good solution - people may use them when they should use a
single, set-based statement such as UPDATE or DELETE.

Of course, there are times when a while or cursor loop is the best
solution, so you might have a good reason to do this - if you can give
more details of what you need to achieve, someone may be able to
comment on whether a loop is appropriate for you or not.

Simon|||Mostly it pays to find a set based solution rather than write loops. If
you need to loop then TSQL has WHILE.

--
David Portas
SQL Server MVP
--|||I try to substitute "for" loop by this excample:

declare @.i int
while (@.i<10)
begin
insert into dbname.dbo.table_name values @.i
@.i=@.i+1
end

but in line @.i=@.i+1 ocured an error. Why ?
I know - it is stupid reason to use "for" loop , but what shoul I changed to
work this example ?
What are better ways to do such task ?|||A common solution is to keep a table of numbers in your database
(single column of numbers from 0 to some very large number). This can
help avoid loops in many places:

INSERT INTO table_name (...)
SELECT num, ...
FROM Numbers
WHERE num BETWEEN 1 AND 10

Of course you'll still probably use a loop to populate the Numbers
table, but that only has to be done once and at install time, not at
runtime.

--
David Portas
SQL Server MVP
--|||Numbers table:

CREATE TABLE numbers (num INTEGER NOT NULL CONSTRAINT pk_numbers
PRIMARY KEY) ;

INSERT INTO numbers (num) VALUES (0) ;

WHILE (SELECT MAX(num) FROM numbers)<65535
INSERT INTO numbers
SELECT num+(SELECT MAX(num)+1 FROM numbers)
FROM numbers ;

--
David Portas
SQL Server MVP
--|||As David suggested, a numbers table is definitely a useful tool:

http://www.aspfaq.com/show.asp?id=2516

A calendar table is another good one:

http://www.aspfaq.com/show.asp?id=2519

Simon|||Hi David,
We can do this simple trick

Select identity(int,1,1) MyId into MyTable from
anySystemTable,anySystemTable,anySystemTable ......

With warm regards
Jatinder Singh|||adam (ereuseen@.wp.pl) writes:
> I try to substitute "for" loop by this excample:
> declare @.i int
> while (@.i<10)
> begin
> insert into dbname.dbo.table_name values @.i
> @.i=@.i+1
> end
> but in line @.i=@.i+1 ocured an error. Why ?

You need to say SET or SELECT in front. I prefer SELECT, as you can assign
more that one variable in one bang:

SELECT @.a = 1, @.b = @.c + d, @.t = NULL

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Here is a way to gemerate the Sequence table without proprietary code
and create the rows in parallel instead of one at a time.

WITH
(SELECT 0
UNION ALL
SELECT 1
..
UNION ALL
SELECT 9) AS Digits(digit)
SELECT D1.digit + 10*D2.digit + 100*D3.digit + 1000*D4.digit + ..
FROM Digits AS D1, Digits AS D2, Digits AS D3, Digits AS D4, ..
WHERE (D1.digit + D2.digit + D3.digit + D4.digit + ..) > 0;

IDENTITY has be done one at a time, while this can be parallelized and
can generate numbers in sets. The CTE can be repalced with a TABLE or
VIEW.

Friday, January 27, 2012

"..Transform requires script Text and Language.." error?

Hi,
I'm trying setup a package to replicate a database table into a another
database's table.
I thought a Data Driven Query Task would work. I got as far as making the
two connections and setting up the Data Driven Query's source SELECT (with
stored procedure I created in the source database) and destination table
under the Bindings tab.
However, when I run the task I get the error below
"ActiveX Scripting Transform requires script Text and Language and at least
one Phase function to be specified."
When I preview the data in under the task's Source tab, I see data AND the
destination table gets created but not data gets transfered. Can someone
please help on what this error means?
Also, I eventually want to update this destination table with several other
subsequent queries from other databases in order to conslidate related data
into one large reporting table but I have to get the initial population of
data to work first. This will be refreshed daily
Does this approach/package task seem to be applicable?
Thanks, Dave.
Dave,
you'd be best off posting in the separate DTS discussion group.
Rgds,
Paul Ibison
|||Open up your package, save it as a bas file and post it here.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
|||I have the exact same problem as this guy. Could you please help?
thanks
marlene
url:http://www.ureader.com/msg/1156295.aspx