Showing posts with label ive. Show all posts
Showing posts with label ive. Show all posts

Tuesday, March 27, 2012

Create a variable type TABLE

I’ve got some tables with the year is part of the name, for example: TABLE2006, TABLE2007, etc.. .The year of the name of table I will read in the table INSERTED of my Trigger : I nead to create a trigger where I update those tables :

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TRIGGER [TESTE]

ON[dbo].[TABTESTE]

FOR INSERT

AS

DECLARE

@.YearTablenvarchar(4),

@.IdClientINT,

@.MyTableTABLE

(

IdClientINT,

SituNVARCHAR(50)

)

BEGIN

SET NOCOUNT ON;

SELECT @.YearTable = SITUACAO, @.IdClient = IdClient FROM INSERTED

SET @.MyTable = 'TABLE' & @.YearTable

UPDATE@.MyTable

SET

Situ= 'X'

WHEREIdClient = @.IdClient

END

GO

Erros:

Msg 156, Level 15, State 1, Procedure TESTE, Line 9

Incorrect syntax near the keyword 'TABLE'.

Msg 137, Level 15, State 1, Procedure TESTE, Line 17

Must declare the scalar variable "@.MyTable".

Msg 1087, Level 15, State 2, Procedure TESTE, Line 18

Must declare the table variable "@.MyTable".

I don't have much experience working with triggers but here's a shot...

-Try changing the @.MyTable to varchar(100)

-Declare another variable @.sql varchar(500)

set @.sql = 'update ' + @.MyTable + ' set situ = '''X''' where IdClient = ' + @.IdClient

exec (@.sql)

You may need to play with the number of single quotes around X to get the string to build correctly.

|||

can you explain what are you trying to do with this statement.

SET @.MyTable = 'TABLE' & @.YearTable

Are you trying to concatenate the string?

|||It seems to me that he thinks that TABLE variables are some kind of references/pointers or interfaces, and that he can use TABLE var with the existing table ('TABLE' & @.YearTable) of the identical structure as TABLE var.
I guess that he, in fact, wants to create and execute some dynamic SQL string based on the inserted values.|||TABLE var is a table just like any "normal" table, it resides in the memory or in tempdb. It si ont some kind of reference!
You can't set it to a string, just like you can't do that with any "normal" table.

Are you trying to update the appropritate table ('TABLE' & @.YearTable) depending on the inserted value (@.YearTable = SITUACAO ... FROM INSERTED)?

|||

hi MauricioBogo,

declare @.MyTable Table

(

IdClient int,

Suit nvarchar(50)

)

after you declare @.MyTable as a "Table"

in this Transaction, the @.MyTable is a "local database object" Already.

not a variable value.

this is why you can't do this → " SET @.MyTable = 'TABLE' + @.Yeartable "

and update @.MyTable .

you can try this, as below:

declare @.sSQL varchar(Max)

declare @.Mytable varchar(255)

select @.yeartable = SITUACAO , @.IDClient = IDClient From Inserted

set @.MyTable = 'TABLE' + @.Yeartable

set @.sSQL = 'update ' + @.MyTable + ' set Situ ='X'
exec (@.sSQL)

--or--

or add any script you need.

try it.

hoping this can help.

Best Regrads,

Hunt.

|||

Yes, I'm trying to concatenate the string, but I already changed to + . Right ?

Thanks

|||

Hunt

Ok it works, thanks

Mauricio

|||

Table variables need to be declare alone:

DECLARE

@.YearTable nvarchar(4),

@.IdClient INT

DECLARE

@.MyTable TABLE

(

IdClient INT,

Situ NVARCHAR(50)

)

sql

Saturday, February 25, 2012

cpu at 100% by sqlserver.exe

Hi,
I've been seing some strange things on my sql server. The cpu reaches voor
100% usage, caused by the sqlserver.exe
I been looking at the periods and start/end time of the 100% usage. I cannot
explain it by looking at the start/end times of backup's, large queries etc
etc.
Has anyone seen this ?
Does anyone have an idea what may cause this?
Greetz,
Macdash
(macdash at home dot nl for replies if possible)
Hi
Run profiler and see what is running at that time.
Generaly 100% utilization is caused by a query not having suitable indexes,
but all data is in cache, so the table scan pegs the CPU at 100% as no disk
I/O is waited on.
Regards
Mike
"Erik" wrote:

> Hi,
> I've been seing some strange things on my sql server. The cpu reaches voor
> 100% usage, caused by the sqlserver.exe
> I been looking at the periods and start/end time of the 100% usage. I cannot
> explain it by looking at the start/end times of backup's, large queries etc
> etc.
> Has anyone seen this ?
> Does anyone have an idea what may cause this?
> Greetz,
> Macdash
> (macdash at home dot nl for replies if possible)
>
>
|||High CPU can be caused by many things... including Order by, group by, and
having clauses... You need to run profiler and check the queries that are
running during the period.
If CPU peaks and 100 and stays forever, it could be the slammer virus if you
are not service packed up..
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Erik" <macdash@.hotmail.com> wrote in message
news:%23EkpdAYxEHA.2564@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I've been seing some strange things on my sql server. The cpu reaches voor
> 100% usage, caused by the sqlserver.exe
> I been looking at the periods and start/end time of the 100% usage. I
cannot
> explain it by looking at the start/end times of backup's, large queries
etc
> etc.
> Has anyone seen this ?
> Does anyone have an idea what may cause this?
> Greetz,
> Macdash
> (macdash at home dot nl for replies if possible)
>

Friday, February 24, 2012

Couple of SQL Agent questions

Hi all,
I've got a customer that is creating a LOCAL PACKAGE/SQL AGENT Job to run a
TextImport.exe program on the server, which loads a table and creates a log
file.
They want to be able to print the log file - to a network printer, as the
final step. I do not see PRINT type tasks in the design window. Any
suggestions?
Also, what they have so far runs when they EXECUTE the package from LOCAL
PACKAGES. But when they try to START JOB from SQL AGENT it fails and they
get a message for step 1 saying:
"The step is improperly defined (it needs a valid owner and/or command) and
so could not be run. The step failed."
Thanks.
Here is a suggestion.
Use the command task executing print.exe /d:lpt1 c:\filename.log
Most of the time when executing packages through Agent this is a security
issue.
Tushar
"Steve Z" <szlamany@.antarescomputing_no_spam.com> wrote in message
news:eNuHobsLEHA.2440@.TK2MSFTNGP10.phx.gbl...
> Hi all,
> I've got a customer that is creating a LOCAL PACKAGE/SQL AGENT Job to run
> a
> TextImport.exe program on the server, which loads a table and creates a
> log
> file.
> They want to be able to print the log file - to a network printer, as the
> final step. I do not see PRINT type tasks in the design window. Any
> suggestions?
> Also, what they have so far runs when they EXECUTE the package from LOCAL
> PACKAGES. But when they try to START JOB from SQL AGENT it fails and they
> get a message for step 1 saying:
> "The step is improperly defined (it needs a valid owner and/or command)
> and
> so could not be run. The step failed."
> Thanks.
>

Couple of question about SQL 2000

Hi

I've bin using MySQL and now trying to convert it on to SQL 2000 and i've got a couple of questions

1. Ive got a SQL Script of a database generated by MySQL, can i run this script stright into SQL 2000 and if so how do i go about it ??

2. In my MySQL i have a Gender field which data type is Enum ('M','F'). How do i recreate this type of field in SQL 2000.

Thanks1) Go to start programs/microsoft sql server/query analyzer. Connect to your DB and run the code

2) I would use a bit field, a bit field can be 0 or 1, you can handle the translation in the front end.

HTH|||Hi thanks for your help

just to be a pain, if i wanted 3 or 4 options would the field type be varchar and i'd have the sort the options out in my application

is this right

thanks again

Originally posted by rhigdon
1) Go to start programs/microsoft sql server/query analyzer. Connect to your DB and run the code

2) I would use a bit field, a bit field can be 0 or 1, you can handle the translation in the front end.

HTH|||It's not going to compile...you need to determine the differences and develop a migration plan..

Do a google...

http://www.databasejournal.com/features/mssql/article.php/3087841

coupla sql questions

say i have this file (below) and want to enter it into a table i've made. why cant i do this in query analyzer? and whats the easiest/best way to do it

insert into myTable

values(1,"StaticHtml","StaticHtmlPageControl"
2,"Questions","QuestionsPageControl"
3,"Login","LoginPageControl"
4,"SubmitSurvey","SubmitSurveyPageControl"
5,"Registration","RegistrationPageControl"
7,"SubmitUserAttributesSurvey","SubmitUserAttributesSurveyControl"
10,"RepeatPageGroup","RepeatPage"
11,"ThankYouPage","ThankYouPageControl")

also

whats the easiest way to make a copy of a database with and without the data - on the same server

cheers

Your SQL syntax isn't quite right. You'll need to write an INSERT INTO for each row you want to insert. i.e.

INSERT INTO myTable VALUES(1,"StaticHtml","StaticHtmlPageControl")

INSERT INTO myTable VALUES(2,"Questions","QuestionsPageControl")

You can script the database structure in order to create a blank instance of your database - Enterprise Manager / SQL Management Studio has options for this. To "copy" your database including data, you should take a backup.

|||

Thx man

bit annoying how u have to write insert into for each row, especially if there's heaps of rows.. i was hoping there might have been a better/easier way

i was also hoping you could copy a database like a table eg: select * into db1 from db2 - or something similar

anyway, i'll just keep hoping

cheers

|||

If you're selecting data from one table into another, you can do multiple rows - like this

INSERT INTO tblTable1
SELECT * FROM tblTable2

If the fields don't match, just specify the columns:

INSERT INTO tblTable1 (Field1, Field2, Field3)
SELECT FieldA, FieldB, FieldC FROM tblTable2

Sunday, February 19, 2012

CountRows()

I've got a simple problem, but because this is my first report, I can't quite figure it out and it's driving me nuts!

I've got an ASP.NET web application with an .RDLC report. The report works fine, i.e. it renders data correctly. However, I cannot get the CountRows() function to work. I've got a cell in the group footer with the following:

=CountRows("grpBuyer")

And I'm getting the compiler error:

The value expression for the textbox XXXX has a scope parameter that is not valid...

"grpBuyer" is what is specified in the Grouping/Sorting property of the group header. I've also tried "TableRow2", which seems to be the name of the group header, but it yeilds the same compiler error.

Some please help!

Thanks, Alex

Hello Alex,

You have to pass group name (which by default is somewhat like "<objectname>_Group") to this function, you can get group name by right clicking on the object(table/matrix/list) and then click on "Group Tab" and there you will see all groups name for that object. You should pass exact name of Group, like i did =CountRows("list1_Details_Group") and it should work fine.

hope this will solve your problem.

Friday, February 17, 2012

Counting Items in Categories

Ive got this monster which will give me a parent categoryName and the number of records linked to a child of that category, I want to use it for a directory where the list of categories has the number of records in brackets next to them. Note: a A listing will show up in each category count it is associated with

Like

Accommodation (10)
Real Estate(30)
Automotive(2)
Education(1)...

Select trade_category.iCategory_Name,Listing_category.iPa rentID,count(Listing_category.iCategoryID) as num
from Listing_category,trade_category Where Listing_category.iParentID = trade_category.iCategoryID Group by
Listing_category.iParentID,trade_category.iCategor y_Name
Union ALL
Select Freecategory.sName,Listing_category.iParentID,coun t(Listing_category.iCategoryID) as num
from Listing_category,Freecategory Where Listing_category.iParentID = Freecategory.iFreeID Group by
Listing_category.iParentID,Freecategory.sName

Which Produces

Real Estate 12401 12
Extreme Sports 3 4

I would Like to get the same query to produce a list of all the empty records too.
so
ID Count
Accommodation 6112 0
Real Estate 12401 12
retail 12402 0
Extreme Sports 3 4
Cycling 5 0There is no such concept of an 'empty record'. If you were to describe to me an 'empty record', what would it be? In situations similar to what you describe, a record can have one of the following characteristics:

- Contain null values for one or more of its fields
- Not be returned with respect to some given criteria.

But there is no mention of an 'empty record' in relational theory or in any Database implementation. Looking at your query, I can't think of what it is you're actually trying to achieve, except in the case where a parent category may have no children, you need to return a result set similar to the following:

{ParentID, ChildCount}
ParentA, 0

When viewed in this way, the problem becomes a trivial LEFT JOIN query that will return all rows from set A irrespective of the contents of set B. In your example however, instead of returning the rows from Set B you will just return a count of the rows.

Select
SetA.columnA,
count(SetB.columnA)
from
SetA

left outer join SetB
on SetA.ColumnA = SetB.ColumnB

Remember: Simplification should be the goal of every developer. A paraphrased quote I once heard said: Perfection is reached not when you can no longer add to it, but when you can no longer take anything away.|||Ive found a short term solution will look at speeding it up when I have some spare time, and I have the live version working so it makes a bit more sense.

Start of December