Showing posts with label insert. Show all posts
Showing posts with label insert. Show all posts

Friday, March 30, 2012

Popuate field with value from last row

Does anyone know how
-- during an insert --
to automatically populate a field with the same data as the field in the
last inserted row?
Thanks.rmg66 wrote:
> Does anyone know how
> -- during an insert --
> to automatically populate a field with the same data as the field in
> the last inserted row?
>
First, you need a way to identify the last inserted row. is there an
InsertionDate column?
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||"rmg66" <rgwathney__xXx__primepro.com> wrote in message
news:%23ozT1swBGHA.208@.tk2msftngp13.phx.gbl...
> Does anyone know how
> -- during an insert --
> to automatically populate a field with the same data as the field in the
> last inserted row?
> Thanks.
SQL server has no concept of Last Inserted Row.
If you have a column on the table that is populated with the date and time
the row was inserted, you could query that row. But it's not impossible that
2 or more rows can be inserted at exactly the same time.
Also, in your case, what happens if the value later changes in that Last
Inserted Row.
If you tell us what the logic is behind what you're trying to do, someone
here could perhaps suggest a better solution.|||> Does anyone know how
> -- during an insert --
> to automatically populate a field with the same data as the field in the
> last inserted row?
Since a table is an unordered set of rows, how do you define "last"?
And why not keep this "last" value in another table?|||Does anyone know why
-- during an insert --
you would want to do this.
I mean if you are doing a select into or and insert select then you can
choose the data you want, if you are doing simple insert stmts. then you
already know what the data is.
post some ddl and maybe we can help
"rmg66" wrote:

> Does anyone know how
> -- during an insert --
> to automatically populate a field with the same data as the field in the
> last inserted row?
> Thanks.
>
>|||You could use a insert trigger on the table.
If value is specified add it as an extended property to the table.
If the value is not specified lookup the extended property on the table.
John
"Bob Barrows [MVP]" <reb01501@.NOyahoo.SPAMcom> wrote in message
news:OOr62uwBGHA.412@.TK2MSFTNGP15.phx.gbl...
> rmg66 wrote:
> First, you need a way to identify the last inserted row. is there an
> InsertionDate column?
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>|||John Kendrick wrote:
> You could use a insert trigger on the table.
>
Why are you replying to me?
I'm not the OP ;-)

> If value is specified add it as an extended property to the table.
> If the value is not specified lookup the extended property on the
> table.
Which gets us back to:
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||> If value is specified add it as an extended property to the table.
> If the value is not specified lookup the extended property on the table.
What does an extended property have to do with data in the table?|||You could create an extended property for each columns that is use the last
inserted record.
When the insert trigger fires it will lookup and use that last column value.
The extended property is an alternative way to handle getting the last
record value without querying the table. Otherwise you would need some way
to find the latest record. Like max(datetime) as Bob mentioned.
John
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23S$7lDxBGHA.3840@.TK2MSFTNGP15.phx.gbl...
> What does an extended property have to do with data in the table?
>|||>> You could create an extended property for each columns that is use the
Can you please elaborate on this approach a bit? Perhaps an example would be
helpful.
Anith

Wednesday, March 21, 2012

Plz help me! I want to use SQL Server2000 DTS Import/Export Wi

I'd still suggest using either a bulk insert or bcp. After your last
explanation, maybe bcp would be better. It'll definitely be faster than
inserting a row at a time. First of all, you can do massive inserts without
logging which will reduce overhead and speed the process. Second, you can
write your C# code to change the bcp command line, for example, change the
table name or the test file path/name according to whatever parameters you
want. In your program, write the bcp command to a windows .bat file then
execute a shell to run the bat if that makes more sense. That way you have
the flexibility to change the insert parameters on the fly and you get the
speed of bcp inserts.
Other than performing an import from text to sql table, what do you mean by
this statement?
"i want to get the functionality of DTS from C#. i mean i want
to use SQL Server objects and libraries to perform SQL server related
tasks from my application."
"Adnan" wrote:

> thanks for sparing time for me.
> actually i have log files that are very huge sized like 700MB or even
> sometimes these reach upto GBs.
> i want to extract information from these files u can say i want to
> apply filters to get only required rows. log files are maintained by
> IIS and contain space seperated fields in each row and there are
> millions of rows.
> now currently i m reading file line by line and inserting that line
> into a database table. after that i select data from that table using
> different filtering criteria and then binding that selected data to a
> data grid.
> now the problem is when i use huge sized files the process becomes very
> slow and user hav to wait for a long time so that the file to be read
> line by line and get inserted into database line by line.
> there are different sample codes that are related to execute DTS
> packages. i want to get the functionality of DTS from C#. i mean i want
> to use SQL Server objects and libraries to perform SQL server related
> tasks from my application.
> i hope that now u hav understood the scenario. the other thing is if i
> create a DTS package from sql server and while executing it i want to
> change its attributes like file name at run time so that i give file
> name as a parameter and the package with that file name get executed.
> if u still hav confusions abt understanding scenario then plz do tell
> me.
> thanks.
> best regards.
>BCP is very much like "bulk copy" technically, but BCP is a dos command. Yo
u
can read all about it in the Sql Server Books Online.
What I'm suggesting is that you can write your C# code to dynamically create
or edit a BCP command in a .bat file (technically it's just a text file).
You can manipulate the file/folder name or even the sql server table name
that the BCP will load if that makes sense in your application. You can set
it to load a table from a formatted or delimited file. For example, you sai
d
you have a space separated file.
bcp is pretty easy to set up and runs quickly.
A simple example of a bcp command would look like this:
bcp "database1.dbo.tbl_target_table" in "c:\foldername\file1.txt"
-c /S"servername" /U"userid" /P"userpassword" -f"c:\foldername\formatfile.fm
t"
In this example, the process is to load an existing sql table
(tbl_target_table) from a text file (file1.txt) using a format file that you
pre-created (formatfile.fmt)
There are many options for BCP depending on your needs. You would be
interested in the -t (field terminator character) and the -r (row terminator
character) which is where you'd tell it that a space is a field terminator
and a Cr/LF is the row terminator. As long as you can run a dos command fro
m
within C#, you can do this. BCP is part of Sql Server, not .Net per se, so
I
don't think you'll have any problems with your version.
"Adnan" wrote:

> hy
> thanks again :-)
> r u talking about SqlBulkCopy? is it BCP? as far as i know about
> it(SqlBulkCopy), it is supported in version 2 of Dot net and in my
> company we r using version 1.1. so if BCP is SqlBulkCopy then it is not
> possible now. and if this is other thing then i m going to check it and
> plz i'll b very thankful to u if u provide me related web links or
> material along with ur valuable suggestions.
> as far as my statement is concerned actually i was going to use SQLDMO
> and finding solution via SQLDMO. at present i hav to only import txt
> files but in future may b i need it.
> i m really thankful to u that u spared ur time for me because i have
> posted this problem at different forums but u r the only ho replied.
> my email id is "adnan.developer@.gmail.com" if u like to attach some
> useful material.
> best regards.
>|||Adnan,
Sorry, I can't help you with C# code. I don't use it. But I've done
similar processes from VB. I suggest you try a different MS community for C
#
programming help. Ask about opening a Dos command shell from c#. I'm sure
you can do this, and it's probably not very difficult.
Microsoft's sites will mostly just describe the command and options, as you
found. They will not help much if you are looking for specific code. But,
there are some good IT community sites other than Microsoft that offer tips
and code for just about any languange. You can search for them on the web
probably find something useful quickly. Use your favorite search engine and
find "C# programming examples" Here's one I like to use: www.ittoolbox.com
You need to know two main programming concepts: 1 how to create/update a
text file from within c#. 2: how to open a dos command shell and execute a
.bat or .cmd command file.
To use it, your application must know how to set up the BCP. that is, your
app must know the pathname/file name of the the text data file an of the BCP
.bat file. That's up to you to determine. Next, you would need to write a
BCP command to the .bat file using the the path/file name of the data, and
the SQL server, database, table, etc. that will be loaded. Last, you need t
o
open a dos command shell, and tell it to execute the BCP .bat file. All of
this can be done in the application without the user intervention if that's
what you need to do.
Good luck.
"Adnan" wrote:

> hello
> how r u tthrone?
> i hav read ur suggestion and i think it is v close to my solution but
> one thing that i hav to telll u that i m not an expert of .net i m new
> to it.
> as i found abt BCp it is a command line utility as u also told. now the
> problem is that from where i can execute it? i mean i m going to make a
> GUI windows based application and i m confudsed that how to implement
> it in my windows based application. plz help me regarding:
> if BCP is a command line utility then fromn which command prompt i can
> execute it? .net command prompt or which.
> and how and where i hav to use it in my application. i dont want my
> user to go to any command prompt.
> actually as it looks i m not clear about BCP that how it works and how
> it is used.
> i'll b very very thankful to u if u help me in this regard because i
> have already taken very much time from company for this.
> plz if it is possible then do provide me some sort of code or at least
> try to clear the concept that what steps should b taken to use BCP from
> my C# application.
> or if u know the link of such a web page then plz tell me because i hav
> searched my best but couldnt find satisfactory help. i hav already
> searched MSDN library it only tell about BCP but doesnt tell that how
> to use it in my application. it just tells its syntax and parameters
> details.
> best regards.
> Adnan Akram.
>

Tuesday, March 20, 2012

Plz Clarify my query in SSIS

Hi There,

I have Task in SSIS to import the flat file to the table and break the table and insert and update the other tables in the same database.

I searched every where i couldnt find appropriate tutorial for this.

Please advice me how to achieve this task.

Thanks in Advance

Regards

Savera

Savera wrote:

Hi There,

I have Task in SSIS to import the flat file to the table and break the table

What do you mean by "break the table"?

Savera wrote:

and insert and update the other tables in the same database.

What other tables? where are you inserting FROM?

Savera wrote:

I searched every where i couldnt find appropriate tutorial for this. Please advice me how to achieve this task.

Which bit?

Savera wrote:

Thanks in Advance

Regards

Savera

-Jamie

|||Load the table first in one data flow. Then in a second data flow, select from that table as the source. Use a conditional split transformation to direct the rows to other destinations based on your criteria.|||

HI Jamie,

Thanks a lot.

I have some more queries to clarify.

I have some complicated criteria like join the tables and set the flag in main table if any of the table updation tasks fails.Will it be done in Conditional split Transformation if its not please let me know how to get this task done.

Thanks in Advance.

Regards

|||

Savera wrote:

HI Jamie,

Thanks a lot.

I have some more queries to clarify.

I have some complicated criteria like join the tables and set the flag in main table if any of the table updation tasks fails.Will it be done in Conditional split Transformation if its not please let me know how to get this task done.

Thanks in Advance.

Regards

To set a flag in a table you would probably use an Execute SQL Task. Put an OnError precedence constraint between your data-flow and this new Execute SQL Task. Something like that anyway - I don't really understand why you're talking about a conditional split.

-Jamie

|||

Jamie,

Thanks for the reply.

I wanted to ask you that whether complicated query can be set in conditional split transformation or have to use Execute sql task ?

Please let me know how to put an OnError precedence constraint between your data-flow and this new Execute SQL.

Ans also i am getting an error while mapping between flat file to database tables the error is column cannot convert between unicode and non unicode string data types.Please advice how it can be solved.

Many thanks in Advance.

Regards,

|||

Savera wrote:

Jamie,

Thanks for the reply.

I wanted to ask you that whether complicated query can be set in conditional split transformation or have to use Execute sql task ?

This makes no snse. You cannot execute queries from the Conditional Split component.

Savera wrote:

Please let me know how to put an OnError precedence constraint between your data-flow and this new Execute SQL.

Drag a precedence constraint between the two tasks (it will be OnSuccessby default) and edit the properties of it. Make it an OnError constraint.

Savera wrote:

Ans also i am getting an error while mapping between flat file to database tables the error is column cannot convert between unicode and non unicode string data types.Please advice how it can be solved.

Somewhere you are trying to push a unicode value into a non-unicode field. You should use a Data Conversion component to explicitly make the change that you need.

-Jamie

|||Savera,
Please read through my posts in this thread. I think you should go through some tutorials so that you can obtain a better understanding of SSIS.|||

Phil Brammer wrote:

Savera,
Please read through my posts in this thread. I think you should go through some tutorials so that you can obtain a better understanding of SSIS.

Agreed.

This may be of use:

Online Beginner Resources
(http://blogs.conchango.com/jamiethomson/archive/2007/01/30/SSIS_3A00_-Online-Beginner-Resources.aspx)

-Jamie

Pls Help-2000 Replication

How do I know whether the record in a row was insert,update and delete that replicated to a subscriber?
Is there a flag or an indicator in msmerge_contents,msmerge_tombstone ,msmerge_genhistory tables or any other msdb/replication system tables?You would have to catch the commands before they got deleted out of Distribution database with the system stored procedure called sp_browsereplcmds. Or you can stop the distribution clean up job temporarily to track it down. The system stored procedure can only be executed in the Distribution database. You have to look for the Commands column and see what execute the command, sp_MSDel_, sp_MSIns_, or sp_MSUpd_.|||Hi Joej,

Is there a table that specifically store the flag a changes occured in Subscriber or Publisher database? If I stop the distribution clean up job, which tables can I refer to?
Thanks

Originally posted by joejcheng
You would have to catch the commands before they got deleted out of Distribution database with the system stored procedure called sp_browsereplcmds. Or you can stop the distribution clean up job temporarily to track it down. The system stored procedure can only be executed in the Distribution database. You have to look for the Commands column and see what execute the command, sp_MSDel_, sp_MSIns_, or sp_MSUpd_.|||Sory I left out a sentence. Let say, just to know whether the records in MSMERGE_CONTENTS and MSMERGE_TOMBSTONE has been send over to replicate on Publisher or Subscriber.

Thanks again!

Originally posted by eshl
Hi Joej,

Is there a table that specifically store the flag a changes occured in Subscriber or Publisher database? If I stop the distribution clean up job, which tables can I refer to?
Thanks|||msrepl_commnads and msrepl_transactions.|||I just tried to insert,update and delete, and I stop the Agent that clean the distribution database. I couldnt find any record in msrepl_commands and msrepl_transactions.

Originally posted by joejcheng
msrepl_commnads and msrepl_transactions.|||back to this issue, I could find the history record that stored the latest record that was merged in th publisher and subscriber. By using this, I will be able to know which record was generated and which wasnt. This is helpful in determining the records that was replicated or not yet.
I m looking into modifying the replication engine,MSSQL replication engine is flexible.

Originally posted by eshl
I just tried to insert,update and delete, and I stop the Agent that clean the distribution database. I couldnt find any record in msrepl_commands and msrepl_transactions.

Wednesday, March 7, 2012

pLEASE HELP:URGENT please

I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?
Thanks in advance
NikiIt automatically fires. Here's a rough example:
create trigger tru_MyTable on MyTable
after update
as
if @.@.ROWCOUNT = 0
return
insert MyOtherTable
select
*
from
inserted
go
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"nikila" <nikilav@.yahoo.com> wrote in message
news:O%232LMtoVFHA.1384@.TK2MSFTNGP09.phx.gbl...
I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?
Thanks in advance
Niki|||Hi Nikila
The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored procedu
re
There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.
U need a "FOR UPDATE" trigger in your situation
Here is how you do it:
CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS
INSERT INTO TABLE-2 SELECT * FROM TABLE-1
GO
For more information about triggers, you can refer:
http://msdn.microsoft.com/library/d... />
2_7eeq.asp
best Regards,
Chandra
http://chanduas.blogspot.com/
---
"nikila" wrote:

> I am new to sql server . I have to write a trigger to insert the data int
o
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when firs
t
> table gets updated?
> Thanks in advance
> Niki
>
>|||Uh, this trigger would insert the entire contents of TABLE-1 into TABLE-2
every time TABLE-1 was updated.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Chandra" <Chandra@.discussions.microsoft.com> wrote in message
news:A4F71FF9-CD9F-45B7-A95F-C728E89B156D@.microsoft.com...
Hi Nikila
The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored
procedure
There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.
U need a "FOR UPDATE" trigger in your situation
Here is how you do it:
CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS
INSERT INTO TABLE-2 SELECT * FROM TABLE-1
GO
For more information about triggers, you can refer:
http://msdn.microsoft.com/library/d... />
2_7eeq.asp
best Regards,
Chandra
http://chanduas.blogspot.com/
---
"nikila" wrote:

> I am new to sql server . I have to write a trigger to insert the data
> into
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when
> first
> table gets updated?
> Thanks in advance
> Niki
>
>

pLEASE HELP:URGENT please

I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?
Thanks in advance
NikiIt automatically fires. Here's a rough example:
create trigger tru_MyTable on MyTable
after update
as
if @.@.ROWCOUNT = 0
return
insert MyOtherTable
select
*
from
inserted
go
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"nikila" <nikilav@.yahoo.com> wrote in message
news:O%232LMtoVFHA.1384@.TK2MSFTNGP09.phx.gbl...
I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?
Thanks in advance
Niki|||Hi Nikila
The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored procedure
There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.
U need a "FOR UPDATE" trigger in your situation
Here is how you do it:
CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS
INSERT INTO TABLE-2 SELECT * FROM TABLE-1
GO
For more information about triggers, you can refer
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_create2_7eeq.asp
best Regards,
Chandra
http://chanduas.blogspot.com/
---
"nikila" wrote:
> I am new to sql server . I have to write a trigger to insert the data into
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when first
> table gets updated?
> Thanks in advance
> Niki
>
>|||Uh, this trigger would insert the entire contents of TABLE-1 into TABLE-2
every time TABLE-1 was updated.
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Chandra" <Chandra@.discussions.microsoft.com> wrote in message
news:A4F71FF9-CD9F-45B7-A95F-C728E89B156D@.microsoft.com...
Hi Nikila
The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored
procedure
There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.
U need a "FOR UPDATE" trigger in your situation
Here is how you do it:
CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS
INSERT INTO TABLE-2 SELECT * FROM TABLE-1
GO
For more information about triggers, you can refer:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_create2_7eeq.asp
best Regards,
Chandra
http://chanduas.blogspot.com/
---
"nikila" wrote:
> I am new to sql server . I have to write a trigger to insert the data
> into
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when
> first
> table gets updated?
> Thanks in advance
> Niki
>
>

pLEASE HELP:URGENT please

I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?
Thanks in advance
Niki
It automatically fires. Here's a rough example:
create trigger tru_MyTable on MyTable
after update
as
if @.@.ROWCOUNT = 0
return
insert MyOtherTable
select
*
from
inserted
go
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"nikila" <nikilav@.yahoo.com> wrote in message
news:O%232LMtoVFHA.1384@.TK2MSFTNGP09.phx.gbl...
I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?
Thanks in advance
Niki
|||Hi Nikila
The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored procedure
There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.
U need a "FOR UPDATE" trigger in your situation
Here is how you do it:
CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS
INSERT INTO TABLE-2 SELECT * FROM TABLE-1
GO
For more information about triggers, you can refer:
http://msdn.microsoft.com/library/de...eate2_7eeq.asp
best Regards,
Chandra
http://chanduas.blogspot.com/
"nikila" wrote:

> I am new to sql server . I have to write a trigger to insert the data into
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when first
> table gets updated?
> Thanks in advance
> Niki
>
>
|||Uh, this trigger would insert the entire contents of TABLE-1 into TABLE-2
every time TABLE-1 was updated.
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"Chandra" <Chandra@.discussions.microsoft.com> wrote in message
news:A4F71FF9-CD9F-45B7-A95F-C728E89B156D@.microsoft.com...
Hi Nikila
The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored
procedure
There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.
U need a "FOR UPDATE" trigger in your situation
Here is how you do it:
CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS
INSERT INTO TABLE-2 SELECT * FROM TABLE-1
GO
For more information about triggers, you can refer:
http://msdn.microsoft.com/library/de...eate2_7eeq.asp
best Regards,
Chandra
http://chanduas.blogspot.com/
"nikila" wrote:

> I am new to sql server . I have to write a trigger to insert the data
> into
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when
> first
> table gets updated?
> Thanks in advance
> Niki
>
>

Saturday, February 25, 2012

Please help: insert into table in cursor-loop?

Hi, all,

I'd like to insert returned items into the result table @.r:

Create function get_items

returns @.r table(a1 varchar(30),a2 varchar(30),a3 varchar(30),a4 varchar(30),a5 varchar(30))

as

begin

declare @.v_item nvarchar (30);

declare @.v_count int;

declare cur_items cursor for

select top 5 a.item from itemtable a; -- gets max. 5 items!

open cur_items;

fetch next from cur_items into @.v_item;

set @.v_count = 0;

while (@.@.fetch_status = 0)

begin

set @.v_count = @.v_count+1;

-- Problem:

-- Insert into @.r(a1,a2...) values(@.v_item)... ?

--

fetch next from cur_items into @.v_item;

end;

close cur_items;

DEALLOCATE cur_items;

return

END

=========

That means,

if @.v_count = 1,

@.r has only one item, such as: 'item1', <null>, <null>, <null>, <null>

but if @.v_count = 5,

@.r has full-row, such as: 'item1', 'item2', 'item3', 'item4', 'item5'

Thank you very much in advance!

If I understand your problem correctly, you don't need a cursor. Use a table valued function (TVF).

Something like this:

CREATE FUNCTION Get_Items ()
RETURNS table
AS
RETURN
( SELECT TOP 5 Item
FROM ItemTable
)

GO

|||

Hello, Arnie Rowland, thanks for your answer!

In my code I have to use the cursor, the definition of the cursor above is just example,

and the result from the cursor is: 'item1', 'item2'... or more, but max. 5 items.

Best regards

|||

Hi, all,

maybe I have to convert all rows of the result table to one row?

after execute the function I got e.g. 3 rows:

item1

item2

item3

==>

if I can convert them to one row, then I have:

item1 | item2 | item3 | <null> | <null>

How can I get it?

Best regards

|||

Here are some resources that may help you get your desired output.

I highly recommend NOT using a cursor if at all possible -and in almost all data retrieval situations, it is possible!

Lists -Field Concatenation
http://groups.google.com/group/microsoft.public.sqlserver.programming/msg/2d85bf366dd9e73e
http://milambda.blogspot.com/2005/07/return-related-values-as-array.html

Lists -Field Concatenation( For SQL 2000 & 2005 )
http://groups.google.com/group/microsoft.public.sqlserver.programming/msg/7e5b4c8a9b9b968a

Lists -Field Concatenation, One Field to Itself for string
SQL 2000
http://omnibuzz-sql.blogspot.com/2006/06/concatenate-values-in-column-in-sql.html
SQL 2005 http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/07/06/871.aspx
http://www.projectdmx.com/tsql/rowconcatenate.aspx

Lists -Recursive Queries
http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=9
http://www.yafla.com/papers/sqlhierarchies/sqlhierarchies.htm
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlpro03/html/sp03i8.asp
http://www.wwwcoder.com/main/parentid/191/site/1857/68/default.aspx
http://www.sqlservercentral.com/columnists/fBROUARD/recursivequeriesinsql1999andsqlserver2005.asp

Monday, February 20, 2012

Please help! Insert a picture in an MS SQL Server table

Please help! Insert a picture in an MS SQL Server table
How can u insert a picture an an image field in SQL server table? Is it
possible from SQL server tools or other tools, programming?
You have a tool, search for textcopy.exe
If you are writing client applications for SQL Server search your
documentation for BLOB keyword.
Milan Kosanovic
"De kess" <Dekesse@.anonyme.com> wrote in message
news:x8pFf.21$71.90552@.wagner.videotron.net...
> Please help! Insert a picture in an MS SQL Server table
> How can u insert a picture an an image field in SQL server table? Is it
> possible from SQL server tools or other tools, programming?
>

Please help! Insert a picture in an MS SQL Server table

Please help! Insert a picture in an MS SQL Server table
How can u insert a picture an an image field in SQL server table? Is it
possible from SQL server tools or other tools, programming?What programming language are you using ?
jens Suessmeyer.|||De kess (Dekesse@.anonyme.com) writes:
> Please help! Insert a picture in an MS SQL Server table
> How can u insert a picture an an image field in SQL server table? Is it
> possible from SQL server tools or other tools, programming?
Yes, this is possible. Unless the pictures are small, you would use
the image data type on SQL 2000 and the data type binary(MAX) in SQL 2005.
You would do this programattically. There is no tool that permits you
just paste in the picture, at least not that comes with SQL Server. There
could be third-party tools out there.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||txs for your answer
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns9761E07E5FF89Yazorman@.127.0.0.1...
> De kess (Dekesse@.anonyme.com) writes:
> Yes, this is possible. Unless the pictures are small, you would use
> the image data type on SQL 2000 and the data type binary(MAX) in SQL 2005.
> You would do this programattically. There is no tool that permits you
> just paste in the picture, at least not that comes with SQL Server. There
> could be third-party tools out there.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx

Please help! Insert a picture in an MS SQL Server table

Please help! Insert a picture in an MS SQL Server table
How can u insert a picture an an image field in SQL server table? Is it
possible from SQL server tools or other tools, programming?You have a tool, search for textcopy.exe
If you are writing client applications for SQL Server search your
documentation for BLOB keyword.
Milan Kosanovic
"De kess" <Dekesse@.anonyme.com> wrote in message
news:x8pFf.21$71.90552@.wagner.videotron.net...
> Please help! Insert a picture in an MS SQL Server table
> How can u insert a picture an an image field in SQL server table? Is it
> possible from SQL server tools or other tools, programming?
>